Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Validate Indian Mobile Number

Posted by Venkat | Labels: ,

Hi , Now i am going to discuss about how to validate a Indian Mobile Number.

First the Mobile Number field should not be null so for that - Add RequiredFieldValidator.

It should not accept any Aplhabets , or any special characters for this you have to write RegularExpression Validator to Accept only Numbers
EX:

Regex \d+


Atlast i have to check whether user entered Number is 10 digit or not and also it should be valid Mobile Number
Regex ^[9][0-9]{9}$


This Regex which first digit should be 9 then followed by 9 digits and totally it accept only 10 digits.

Validation which accept( empty , numeric or Float values)

Posted by Venkat | Labels: ,

Now we have to discuss about the validationexpression ie:

the textbox should accept empty values , numeric ( float ) .

Implementation Code :

^\d*\.?\d*$

HTML


<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Invalid format" ControlToValidate="TextBox3" ValidationExpression="^\d*\.?\d*$" ValidationGroup="d"></asp:RegularExpressionValidator>
<asp:Button ID="Button5" runat="server" Text="Button" ValidationGroup="d" />
Matches

( ) -> empty value
4535
520.20
2.54

Non-Matches

21.0dfdf
47854
Rpks

Validate Decimal numbers

Posted by Venkat | Labels: ,

Now we are going to discuss about the validate the decimal numbers it should be in the format of


<asp:TextBox ID="Phone_TextBox" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"
ValidationExpression="^\d+\.\d{2}$" ValidationGroup="phone" ControlToValidate="Phone_TextBox">it should be 125.00 this format</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="phone" /></div>


Matches

142.00
254.25 etc

Non-Matches

214
25.0
254.5
254.355

code:

Validate UniqueIdentifier

Posted by Venkat | Labels: ,

Here we have to discuss about how to validation UniqueIdentifiere using REGEX


Here is the Expression


^\w{7}-\w{4}-\w{4}-\w{4}-\w{12}

Validate the numbers

Posted by Venkat | Labels: ,

It wont allow spaces before or after the digits but it wont if there are only spaces so you need RequiredFieldValidator too
the RegularExpressionValidator to validate the phone number without using JavaScript. So something like this:

<asp:regularexpressionvalidator id="RegularExpressionValidator1">
ControlToValidate="TextBox1"
ValidationExpression="\d+"
Display="Static"
EnableClientScript="true"
ErrorMessage="Please enter numbers only"
runat="server"/>

Textbox should allow minimum 5 Character

Posted by Venkat | Labels: ,

Hi Use this code.


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator
ID="valUserName" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" ErrorMessage="Minimum length 6 characters"
ForeColor="" ValidationExpression=".{6}.*" ></asp:RegularExpressionValidator>

Limit the Maximum number of Characters in the texbox

Posted by Venkat | Labels: , ,

Here is the code how to limit the no.of charater entered by the user in TextBox , mostly this can be implemented on Post comment , Feedback , Message board ...



This example demonstrates on how are we going to limit the number of characters to be typed into the TextBox using JavaScript and display the remaining characters in a Label Control.


<script type="text/javascript" language="javascript">

function validatelimit(obj, maxchar)
{

if(this.id) obj = this;

var remaningChar = maxchar - obj.value.length;
document.getElementById('<%= Label1.ClientID %>').innerHTML = remaningChar;

if( remaningChar <= 0)
{
obj.value = obj.value.substring(maxchar,0);
alert('Character Limit exceeds!');
return false;

}
else
{return true;}
}

</script>

<body runat="server">
<form id="form1" runat="server">

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onkeyup="validatelimit(this,300)"> </asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="0"></asp:Label>

</form>

</body>
Note: 300 is the MAX length of the characters in the TextBox..Just change the value of 300 based on your requirements..

Validate textbox it should contains atleast 20 characters

Posted by Venkat | Labels:

Here we have to discuss the validation of the field , the textbox should contain atleast 20 Characters


ValidationExpression=^.{20,1999}$ - >

This expression should accept atleast 20 charactes and maximum of 1999 - it does not accept less than 20 characters

If I assumed that you want to enter only alphabets ie a-z into your textbox for which the validation expression is:

ValidationExpression="^\D{20,}$"

For having both alphabets and digits expression:

ValidationExpression="^[a-z0-9]{20,}$"

Your expression below will allow all alphabets, digits as well as special chars as #, $ etc to be entered incase thats what you want:

ValidationExpression="^.{20,1999}$"

Validate URL

Posted by Venkat | Labels: ,

Validate URL 

<asp:RegularExpressionValidator 
            ID="RegularExpressionValidator1"
            runat="server" 
            ValidationExpression="(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"
            ControlToValidate="TextBox1"
            ErrorMessage="Input valid Internet URL!"
            ></asp:RegularExpressionValidator> 
 
Here i update the Regex this will accept the URL like:
http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html 

Validate US Zip Code

Posted by Venkat | Labels: ,

To validate US Zip Code


<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="\d{5}(-\d{4})?"
ControlToValidate="TextBox1"
ErrorMessage="Input valid U.S. Zip Code!"
></asp:RegularExpressionValidator>

Validate US Phone number

Posted by Venkat | Labels: ,

Validate US Phone number


<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
ControlToValidate="TextBox1"
ErrorMessage="Input valid U.S. Phone Number!"
></asp:RegularExpressionValidator>

Validate US Social Security Number

Posted by Venkat | Labels: ,

Here we have to discuss how to validate US Social security number


<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" validationexpression="\d{3}-\d{2}-\d{4}"
controltovalidate="TextBox1" errormessage="Input valid U.S. Social Security Number!"></asp:regularexpressionvalidator>

Validation for Tiny_MceEditor Control

Posted by Venkat | Labels: , ,

Here is the code

validation for tiny_mceEditor control

here is the code for validate the (Required field validation ) for the control

< style="font-weight: bold;">OnClientClick="tinyMCE.triggerSave(false,true);"

Accept a Textbox of 1000 Characters Only

Posted by Venkat | Labels: ,

Suppose in a message board related to same point of view , we place the text with multiline = True.. so user can enter a number of paragraphs, so the user has to avoid more lines ( paragraph ) to allow only the specific Character here the Regular expression to allow only 1000 Characters

EGEX - Validataion Expression - [\\s\\S]{0,1000}$

PayOffers.in