Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Updated 7-Nov-20 22:26pm

Try this:
^[a-zA-Z]+(\s[a-zA-Z]+)?$
 
Share this answer
 
Try this..


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


C#
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
 
Share this answer
 
v3
Comments
sainivip 12-Nov-12 0:49am    
No it's not working
Divya RS 12-Nov-12 1:45am    
the solution has been modified check with it
MT_ 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
you can acheive this task by using below regExp

([A-Za-z])+( [A-Za-z]+)
 
Share this answer
 
^ : start of string
[A-Za-z\s]+ : match one or more occurrences of alphabets (both upper and lower case) and spaces
$ : end of string
This regular expression will only match strings that consist of alphabets and spaces, with no other characters allowed.
 
Share this answer
 
Comments
Dave Kreskowiak 9-May-23 8:16am    
Really? 11 years after the question was asked? Try sticking to questions a bit more recent.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900