Need Code for Validations Using Javascript without using Validation Controls in ASP.NET

Posted by Raghu16 under ASP.NET on 11/14/2011 | Points: 10 | Views : 1492 | Status : [Member] | Replies : 1
validation should be done without using any Validations controls
i mean form is design as

Textbox1(required Field validation)
textbox2(Email Validation)
Textbox3( Numeric validation)
Textbox4(NO special characters validation)
Textbox5(Alpha numeric validation)
Textbox6(Date validation)


here i need code using java script in asp.net without using any Validation controls to toolbox ........

and also give me information how to do in Asp.net
i have try lot but iam not getting solution where javascript code should write and where asp.net code should write. how it works...............



<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript">
<!--
function Form1_Validator(theForm)

{

var alertsay = "";
if (theForm1.Name.value == "")
{

alert("You must enter an Name.");
theForm.Name.focus();
return (false);

}

}
</script>
<head runat="server">

<title><;/title>

</head>

<body>
<form id="form1" runat="server">
<form action="javascript.asp?<%--ID=<%=siteID%>--%>"
method="POST" onsubmit="return Form1_Validator(this)" name="Form1">
<input type="submit" name="Submit" value="Submit">


<asp:Label ID="Label1" runat="server"
style="z-index: 1; left: 42px; top: 291px; position: absolute"
Text="Label"></asp:Label>
<asp:TextBox ID="Name" runat="server"
style="z-index: 1; left: 96px; top: 287px; position: absolute"></asp:TextBox>





</form>
</form>
</body>
</html>


i have written code like this but it not working
pls help me i am new for .Net.........




Responses

Posted by: Sreekanthv5 on: 11/14/2011 [Member] Starter | Points: 25

Up
0
Down
<script type="text/javascript">

function validate() {
if (document.getElementById("<%=txtname.ClientID %>").value == "") {
alert("Name field cannot be blank");
document.getElementById("<%=txtname.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txtemail.ClientID %>").value == "") {
alert("Email id can not be blank");
document.getElementById("<%=txtemail.ClientID %>").focus();
return false;
}
var emailPat = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
var emailid = document.getElementById("<%=txtemail.ClientID %>").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null) {
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtemail.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txturl.ClientID %>").value == "") {
alert("Web URL can not be blank");
document.getElementById("<%=txturl.ClientID %>").value;
document.getElementById("<%=txturl.ClientID %>").focus();
return false;
}
var Url = "^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
var tempURL = document.getElementById("<%=txturl.ClientID%>").value;
var matchURL = tempURL.match(Url);
if (matchURL == null) {
alert("Web URL does not look valid");
document.getElementById("<%=txturl.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txtzip.ClientID%>").value=="")
{
alert("Zip Code is not valid");
document.getElementById("<%=txtzip.ClientID%>").focus();
return false;
}
var digits="0123456789";
var temp;
for (var i=0;i<document.getElementById("<%=txtzip.ClientID %>").value.length;i++)
{
temp=document.getElementById("<%=txtzip.ClientID%>").value.substring(i,i+1);
if (digits.indexOf(temp)==-1)
{
alert("Please enter correct zip code");
document.getElementById("<%=txtzip.ClientID%>").focus();
return false;
}
}
return true;
}

</script>
<fieldset>
<legend> Legend Information </legend>
Name:&nbsp&nbsp&nbsp&nbsp<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
Email:&nbsp&nbsp&nbsp&nbsp<asp:TextBox ID="txtemail" runat="server"></asp:TextBox><br />
Web URL:&nbsp&nbsp<asp:TextBox ID="txturl" runat="server"></asp:TextBox><br />
Zip:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<asp:TextBox ID="txtzip" runat="server"></asp:TextBox><br />
<asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClientClick="return validate();" />


I hope this code helps you.


Raghu16, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response