Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Forums Author
Thu, 24-Jul-2014 Authors
Jayakumars
20
Nayeem@Dotnetfunda
10
Rajkatie
10
All Time Authors
Jayakumars
4120
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)

validate url using javascript [Resolved]

Santosh198318
Posted by Santosh198318 under JavaScript on 12/6/2010 12:00:01 AM | Points: 10 | Views : 2843 | Status : [Member] | Replies : 2


hi.....
in drop down list have some list items like url,email id,phone nor..... i want to validate url and email id using java script.....pleasr provide this code...



Posted by: Lalji_mer on: 12/6/2010 [Member] Bronze | Points: 50

Resolved

show here good sample code
1. Name : <asp:TextBox ID="txtName" />
2. Email : <asp:TextBox ID="txtEmail" />
3. Web URL : <asp:TextBox ID="txtWebUrl" />
4. Zip : <asp:TextBox ID="txtZip" />
5.

<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" />


Now on the source code of this form in script tag write the following code:

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

function validate()
{
if (document.getElementById("<%=txtName.ClientID%>").value=="")
{
alert("Name Feild can not 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 = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\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("<%=txtWebURL.ClientID %>").value=="")
{
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID %>").value="http://"
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url);
if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.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>

javascript:FormatText('U',%20'PlaceHolderForContents_txtAnswer')
Another way visit this link

http://www.mcfedries.com/JavaScript/mandatory.asp

LP MER

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

Posted by: Santosh198318 on: 12/6/2010 [Member] Starter | Points: 25

THANKS I GOT THE SOLUTION

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

Login to post response


Found interesting? Add this to: