Description:- In Asp.net Validation Now we started to cover All Validations used in Asp.net.In my previous Articles We done Email and Phone Number , Web Url Address Validation. Now in this Article We Describe How to use Range Validator Between Two values.So My friends here is the Demo How The Code Works..
Demo:-
Coding:-
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblNum" runat="server">Enter age Between 18 to 28</asp:Label>
<asp:TextBox ID="txtage" runat="server" ></asp:TextBox>
<asp:RangeValidator runat="server" ID="rgvtxtage" ErrorMessage="Please Enter Age Between 18 to 28" MinimumValue="18" MaximumValue="28" Type="Integer" ControlToValidate="txtage"></asp:RangeValidator>
</div>
</form>
</body>
</html>
Code-:
<%@ 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 id="Head1" runat="server">
<title>Vallidations Page</title>
<script language="javascript" type="text/javascript">
function validate() {
var summary = "";
summary += isvalidFirstname();
summary += isvalidEmail();
summary += isvalidphoneno();
if (summary != "") {
alert(summary);
return false;
}
else {
return true;
}
}
function isvalidphoneno() {
var uid;
var temp = document.getElementById("<%=txtphone.ClientID %>");
uid = temp.value;
var re;
re = /^[0-9]+$/;
var digits = /\d(10)/;
if (uid == "") {
return ("Enter Ur Phone No." + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Only Numbers Allowed" + "\n");
}
}
function isvalidFirstname() {
var uid;
var temp = document.getElementById("<%=txtfname.ClientID %>");
uid = temp.value;
var re=/^[a-zA-Z ]+$/
if (uid == "") {
return ("Enter First Name" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Characters and spaces only" + "\n");
}
}
function isvalidEmail() {
var uid;
var temp = document.getElementById("<%=txtEmail.ClientID %>");
uid = temp.value;
var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (uid == "") {
return ("Please Enter Email" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Enter A valid Email" + "\n");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table align="center">
<tr>
<td>
<asp:Label ID="lblfname" runat="server" Text="FirstName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Email"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCnt" runat="server" Text="Phone No"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Save"
OnClientClick ="javascript:validate()" />
</td>
</tr>
</table>
</form>
</body>
</html>
Demo-:
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "Your Home Page:<br/ >" + TextBox1.Text.ToString(); }</script><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title>asp.net RegularExpressionValidator example: how to validate URL</title></head><body> <form id="form1" runat="server"> <div> <h2>RegularExpressionValidator: Internet URL</h2> <asp:Label ID="Label1" runat="server" Font-Bold="true" Font-Italic="true" Font-Size="Large" ForeColor="black" > </asp:Label> <br /><br /> <asp:Label ID="Label2" runat="server" Text="Home Page" Font-Bold="true" ForeColor="Teal" > </asp:Label> <asp:TextBox ID="TextBox1" runat="server" BackColor="Teal" ForeColor="white" Width="250" > </asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Text="*" > </asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" ControlToValidate="TextBox1" ErrorMessage="Input valid Internet URL!" ></asp:RegularExpressionValidator> <br /><br /> <asp:Button ID="Button1" runat="server" Text="Submit URL" Font-Bold="true" OnClick="Button1_Click" /> </div> </form></body></html>
Demo-: