regular expression validate textbox at least two words is required - asp.net

i have a text box and i need validate it with regular expression at least two words in text box, and not contain spaces in the first character. pls give me a regular expression to validate my textbox
Currently I am using
^((\b[a-zA-Z]{2,40}\b)\s*){2,}$
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
controltovalidate="TextBox1" validationexpression="^((\b[a-zA-Z]{2,40}\b)\s*){2,}$"
errormessage="at least two words"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" OnClick="btnClick" runat="server" Text="Button" />

Assuming they can be separated by any whitespace character (space, tab, etc):
^[a-z]+(?:\s[a-z]+)+$
Here's the breakdown:
Assert position at the beginning of the string ^
Match a single character in the range between “a” and “z” [a-z]+
Between one and unlimited times, as many times as possible +
Match the regular expression below (?:\s[a-z]+)+
Between one and unlimited times, as many times as possible +
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) \s
Match a single character in the range between “a” and “z” [a-z]+
Between one and unlimited times, as many times as possible +
Assert position at the end of the string (or before the line break at the end of the string, if any) $
...But if they're separated by only a space:
^[a-z]+(?: [a-z]+)+$
...or if any non-word character for the separator:
^[a-z]+(?:\W[a-z]+)+$
This should be used with RegexOptions.IgnoreCase. For example (in C#):
if (Regex.IsMatch(subjectString, #"^[a-z]+(?:\W[a-z]+)+$", RegexOptions.IgnoreCase)) {
// Successful match
} else {
// Match attempt failed
}

Your are probably looking for this:
^[a-zA-Z]{2,40}(?: +[a-zA-Z]{2,40})+$
Description:
^ # anchor for the start of the string
[a-zA-Z]{2,40} # ascii letters
(?: # open a non-capturing group
[ ]+[a-zA-Z]{2,40} # one or more spaces followed by letters
)+ # repeat the group one or more times
$ # anchor for the end of the string
Note that word boundaries are useless.

Related

RegularExpressionValidator - Validate first 2 characters

I am trying to validate a string entered into a textbox. I want to make sure that the first 2 characters are either 02, 04 or 09.
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "check_number" ID="rxvValidCheckNumber1" ValidationExpression = "^(02|04|09)" runat="server" ErrorMessage="Valid Check Number required."></asp:RegularExpressionValidator>
If I enter a string that begins with 02, 04 or 09 the ErrorMessage still fires. What am I doing wrong?
The ValidationExpression regex is anchored by default, and thus you need to match the entire input. You may match 0+ chars with .*:
ValidationExpression = "^(02|04|09).*"
To make it a bit more "elegant", you may use 0[249] after ^:
ValidationExpression = "^0[249].*"
The expression matches
^ - start of string anchor
0 - a 0 digit
[249] - a character class matching either 2 or 4 or9
.* - any 0+ chars other than line break chars.
If your textobx is multiline, you need to use (?s) singleline/dotall modifier
ValidationExpression = "(?s)^0[249].*"
or (to enable client side validation, the (?s) is not supported in JavaScript):
ValidationExpression = "^0[249][\s\S]*"
where [\s\S] matches any char including a line break char.

Including a specific value restriction in regular expression validator

I'd like to include a restriction to my regex below, so that name, NAME, Name, NaMe, etc. could not pass validation with the following REV:
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="tbAttColName"
CssClass="failureNotification" ErrorMessage="Column can only contain alpha numeric and spaces, and must start with a letter"
ValidationGroup="AddColumn" Text="*" ValidationExpression="^[A-Za-z][0-9A-Za-z ]*$" Display="Dynamic" />
Examples of valid input:
nam
nam1
name1
theName
N1ame
helloname
whatisyourname
Examples of invalid input:
1name
Name
NAME
NaMe
naME
You want to allow any value meeting your pattern excluding a certain value (that is equal to the full string). It means you need to add a negative lookahead anchored at the start and its pattern should be anchored at the end of string with $.
^(?![Nn][Aa][Mm][Ee]$)[A-Za-z][A-Za-z0-9 ]*$
^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?![Nn][Aa][Mm][Ee]$) - the whole string cannot equal a name string (case insensitive)
[A-Za-z] - an ASCII letter
[A-Za-z0-9 ]* - zero or more ASCII letter, digits or spaces
$ - end of string.

Regular expression for password with some characters

I want the regex for password textbox with below criteria
(a) At least One character
(b) At least One special character
(c) At least One numeric value
(d) and the length should be greater than 8 digit
Tried something like below and it worked.
Solution 1:-
function checkPassword(password) {
var pattern = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/;
if (!pattern.test(password)) {
$(".password_error").show();
} else {
$(".password_error").hide();
}
}
Solution 2:-
<asp:TextBox ID="txtPolicy4" runat="server"></asp:TextBox><br />
<asp:RegularExpressionValidator ID="Regex4" runat="server" ControlToValidate="txtPolicy4"
ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]{8,}"
ErrorMessage="Password must contain: Minimum 8 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet, 1 Number and 1 Special Character" ForeColor="Red" />
Both the solution worked for me, but I preferred the second solution.

How to check text contain alphabet and digit

I have a textbox that user enter a string into it.
<td class ="auto-style2" > <asp:TextBox ID="TextBox_PassportCode" runat="server" Width ="100%"></asp:TextBox></td>
and:
string code=TextBox_PassportCode.text;
I want to check if "code" contain alphabet + digit together. Eg,A1234 or 1234A Or fhg21564,
It is not important how many alphabet or how many digit user enter, but textbox should contain alphabet and digit.
Try doing like below. Since you said the text could be either A1234 or 1234A; it not necessary that string starts with alphabet. In which case, you can check against regular expression [a-zA-Z0-9]+ which will match combination of 1 or more alphabet + digits. See MSDN on how to use
string code=TextBox_PassportCode.text;
string pat = "[a-zA-Z0-9]+";
if (System.Text.RegularExpressions.Regex.IsMatch(code, pat, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine("Match Found");
}
else
{
System.Console.WriteLine("No Match");
}
Use a regular expression for this.
See this answer.
C# Regex to allow only alpha numeric

zipcode regular expression validation

I would like to have a regular expression validator for validating zip code. My zip code length varies up to 9 digits. User can enter either 5 or 9. I should valid if he enters 5 digits or 9 digits. Any thing other than that I would like to raise error.
I tried this expression
ValidationExpression="\\d{5}(-\\d{4})?$"
This is my design I am using rad controls
<telerik:RadMaskedTextBox Mask="#####-####" runat="server" ID="txtcontactZipCode"
Width="200px" ValidationGroup="contactValidation">
</telerik:RadMaskedTextBox>
<asp:RequiredFieldValidator runat="server" ID="rqrdcontactZipCode" ValidationGroup="contactValidation" Display="Dynamic"
ForeColor="Red" ControlToValidate="txtcontactZipCode" ErrorMessage="Zip Code is required"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexpcontactZipCode" runat="server" ControlToValidate="txtcontactZipCode"
ValidationGroup="contactValidation" Display="Dynamic" ForeColor="Red" ErrorMessage="Should be 5 or 9 Digits"
ValidationExpression="\\d{5}(-\\d{4})?$"></asp:RegularExpressionValidator>
But I am unable to valid if I enter as follows 11111-____
Can some one help me..
The issue is that your regular expression indicates the four digits must exist if you have the dash. Generally that would be okay but since you're using an input mask the dash always exists, even when it's only five digits. Try the following expression.
ValidationExpression="\d{5}-?(\d{4})?$"
You should only use \\ to escape when you're setting it through C# code-behind.
Use this...
ValidationExpression="\d{5}(-\d{4})?$"
If you were setting it through the C# in the background, then you would need \\d because \d would be considered to be a control character...
txtcontactZipCode.ValidationExpression = "\\d{5}(-\\d{4})?$";
This is unless you precede the string with #, in which case it could be done as...
txtcontactZipCode.ValidationExpression = #"\d{5}(-\d{4})?$";
What about :- [0-9]{5}(\-[0-9]{4})?
[0-9] Any number between 0 and 9, {5} = only 5 characters; Altarnativly \d depending on what you find easier to read.
( ) - Create a group
\-[0-9]{4} A Dash followed by 4 numbers
? Optional - Zero or One
Use this method:
public static boolean validateZip( String zip )
{
return zip.matches( "\\d{5}" );
}

Resources