Regular Expression For Telephone Number - asp.net

I have regular expression in regular expression validator control. It supports telephone number like this for example +359111111111. The symbol + is mandatory. I want to allow with the start of 0 or + and then the phone number.
What I have now is:
<asp:RegularExpressionValidator ID="revGroupTelephone" runat="server" ValidationExpression="^\+[1-9]{1}[0-9]{7,11}$"
Text="*" ErrorMessage="Wrong number" ControlToValidate="tbGroupTelephone" ValidationGroup="validOrg" />

You can use | for an "or" clause : see msdn
So I would replace
\+
by
(\+|0)

Related

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.

regular expression for this format 00-0000000

<td><asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="registry_no"
ErrorMessage="Invalid Format!" ForeColor="#a90329" Font-Size="Small" ValidationExpression="/^\d+(-\d+)*$/"/></td>
regular expression for this format 00-0000000
i want to validate the user textbox input , the format should be like this one : 00-0000000, two digits followed by a dash , followed by 7 digits .
thanks
You can specify the exact number of repetitions with braces, like so:
^\d{2}-\d{7}$

validation expression in asp.net

I am trying to create a validation expression for my regular expression validator control. I am having trouble understanding how to add things to the validation expression. I need it to accept any input (numbers, characters, any special characters) and limit the input to 200. I have this so far:
<asp:RegularExpressionValidator ID="commentRegularExpressionValidator" runat="server" ErrorMessage="Comment box exceeds 200 characters."
ControlToValidate="TextBoxComments"
ValidationExpression="^[a-zA-Z''-'\s\d ^!##$%^&*()_+=-]{0,200}$"
Text="*"
CssClass="errorMessage"
SetFocusOnError="true"
Display="Dynamic"
ValidationGroup="infoGroup"></asp:RegularExpressionValidator>
When I try to add more special characters to the expression, I start getting errors. Can somebody help clarify the format/structure of adding special characters to this validation expression. Thank you
If you need only to accept any input (numbers, characters, any special characters) and limit the input to 200 then Set only property of Textbox like --> MaxLength="200"
Or if you want to show message then use javascript or Jquery like below function on Keydown event of textbox-->
function CountCharacters() {
var maxSize = 200;
if (document.getElementById('<%= txt_handlinginfo.ClientID %>').value != '') {
var len = document.getElementById('<%= TextBoxComments.ClientID %>').value.length;
if (len > maxSize) {
alert('error message');
return false;
}
}
}
You can use .{1,200} regular expression.
. dot character means any character
{1,200} means match 1 or more character upto max 200 characters.
<asp:RegularExpressionValidator ID="commentRegularExpressionValidator" runat="server"
ErrorMessage="Comment box exceeds 200 characters."
ControlToValidate="TextBoxComments"
ValidationExpression=".{1,200}"
Text="*"
CssClass="errorMessage"
SetFocusOnError="true"
Display="Dynamic"
ValidationGroup="infoGroup">
</asp:RegularExpressionValidator>

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