JavaScript Validation for Mobile Number in Asp.Net
Validate Mobile number in a web page using JavaScript validation in ASP.Net.
In this example I am using one TextBox . The TextBox accepts Mobile number, it only allows 10 digit numbers.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mobileValidation.aspx.cs" Inherits="mobileValidation" %>
<!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>Vallidation On Mobile Number</title>
<script type="text/javascript">
function Validate() {
var msg = "";
msg += MobileNumberValidation();
if (msg != "")
{
alert(msg);
return false;
}
else
{
alert("Mobile Number submited successfuly");
return true;
}
}
function MobileNumberValidation() {
var id;
var controlId = document.getElementById("<%=txtMobileNo.ClientID %>");
id = controlId.value;
var val;
val = /^[1-9]{1}[0-9]{9}$/;
if (id == "")
{
return ("Please Enter Mobile Number" + "\n");
}
else if (val.test(id))
{
return "";
}
else {
return ("Mobile Number should be only in digits" + "\n");
}
}
<body>
<form id="form1" runat="server">
<h3 style="color:blue">Validation for Mobile Number in JavaScript</h3>
<table style="border-color: #333300; z-index: 1; left: 15px; top: 54px; position: absolute; height: 122px; width: 261px">
<tr>
<td class="style1">
<asp:Label ID="lblMobileNo" width="138px" runat="server" Text="Mobile Number"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtMobileNo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Button ID="bttnsubmit" runat="server" Text="Submit" OnClientClick ="Validate()" Font-Bold="True" />
</td>
</tr>
</table>
</form>
</body>
</html>
0 Comment(s)