Click here to Skip to main content
11,410,584 members (59,520 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: regular-expression
Hi Need one regular expression which satisfy the flowing words -
1- The
2- The King

Maximum two words and one space is allow between them. please suggest me good regex.
Posted 11-Nov-12 19:21pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this:
^[a-zA-Z]+(\s[a-zA-Z]+)?$
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can acheive this task by using below regExp

([A-Za-z])+( [A-Za-z]+)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this..


This is the javascript that i have used and its working.Try with this


function checkregex() {
            if (!form1.TextBox1.value.match(/^[a-zA-Z ]+$/) && form1.TextBox1.value != "") {
                form1.TextBox1.value = "";
                form1.TextBox1.focus();
                alert("Please Enter only alphabets in text");
            }
        }
here is the regexx to validate one or 2 words with a whitespace between them

(/^[a-zA-Z0-9]+ ?([a-zA-Z0-9]+$){1}/)

Note : you cannot have a whitespace at the end if it is only one word
Try and let me know u got it or not
  Permalink  
v3
Comments
sainivip at 12-Nov-12 0:49am
   
No it's not working
Anusha SR at 12-Nov-12 1:45am
   
the solution has been modified check with it
Milind Thakkar at 12-Nov-12 3:40am
   
THis will check that the string contains only alphabet and space. It will not make sure "Maximum two words and one space is allow between them" condition

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 19 Apr 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100