Click here to Skip to main content
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 18: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 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
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  

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 483
1 OriginalGriff 333
2 Maciej Los 305
3 CHill60 220
4 manoj kumar choubey 184
0 OriginalGriff 6,367
1 Sergey Alexandrovich Kryukov 5,296
2 Maciej Los 3,384
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 19 Apr 2014
Copyright © CodeProject, 1999-2014
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