Powered by Blogger.

Thursday, February 27, 2014

How to validate user input for mobile number using Regular Expression in Javascript?



In this post we will discuss about how to validate user input for mobile number using Regular Expression in Javascript?

Also check out:

- SET XACT_ABORT ON in sql server

- RangeValidator example in Asp.Net

- Interfaces in C#.Net

The below Html code explains the client side validation for mobile number using Regular Expressions.  

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script language="ecmascript" type="text/ecmascript">
              function validateMobileNo() {
                var p1 = /^([0-9]{10})$/;
                var mobile_no = form1.Text1.value;
                var x1 = mobile_no.match(p1);
                if (x1 == null) {
                    alert("Invalid User Input");
                    return false;
                }
                return true;
            }    
    </script>

  </head><body>
    <form id="form1" runat="server">
    <div>
        Enter Mobile Number:
        <input id="Text1"    name="txt1"  type="text" /><br />
        <br />
         
        <asp:Button ID="Button2"   OnClientClick ="return validateMobileNo();" Text="MobileNoCheck"  runat="server" />
     
    </div>
  </form>
</body>
</html>



0 comments

Post a Comment