Collection of Javascript validations

if user give text box as blank means,we will show a alert message throguh java script.check the below code for java script empty validation.

Text Box Empty Validation:-
[code]
function TextboxEmptyValidation()
{
var txt=document.getElementById("TextBox1");
if(txt.value=="")
{
alert("Textbox cannot be blank");
txt.focus();
return false;
}
}
You can call above function like this

<asp:TextBox ID="TextBox1" onblur="return TextboxEmptyValidation()" runat="server"></asp:TextBox>
[/code]

if you want to enter only number for particular textbox means,you will call the below function in textbox onkeypress event.

Text Box Enter only Numbers:-
[code]
function valNumeric(evt)
{

var charCode;
charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode <= 48 && charCode >= 57 || charCode== 8 || charCode == 118 || charCode == 120 || charCode == 99 )
{
return true;
}
else
{
return false;
}
}

You can call above function like this

<asp:TextBox ID="TextBox2" onkeypress="return valNumeric(event)" runat="server"></asp:TextBox>

[/code]

if user does not select value from dropdownlist means,we will show a alert message throguh java script.
check the below code for java script dropdownlist empty validation.

Dropdownlist empty validation

[code]
function DropdownlistEmptyValidation()
{
var dropdown=document.getElementById("DropDownList1");
if(dropdown.value=="-1")
{
alert("dropdownlist cannot be blank");
dropdown.focus();
return false;
}
}
You can call the above function like this

<asp:DropDownList ID="DropDownList1" onblur="return DropdownlistEmptyValidation()" runat="server" AutoPostBack="True">
<asp:ListItem Value="-1">---Select--</asp:ListItem>
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
<asp:ListItem>Item3</asp:ListItem>
</asp:DropDownList>


[/code]

if you want to enter only char for particular textbox means,you will call the below function in textbox onkeypress event.

Enter only char
[code]
function valAlpha(evt)
{

var charCode;
charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode <= 97 && charCode >= 122 || charCode <= 65 && charCode >= 90 || charCode==8)
{
return true;
}
else
{
return false;
}
}
You can call the above function like this

<asp:TextBox ID="TextBox3" onkeypress="return valAlpha(event)" runat="server"></asp:TextBox>
[/code]
if you want to check given website is valid or not,using javascript.take the below code to check valid or not

WebsiteValidation
[code]
function WebsiteValidation(ctrName)
{
var strURL=document.getElementById(ctrName).value;
if(strURL!='')
{
var tomatch= /www\.[A-Za-z0-9\.-]{2,}\.[A-Za-z]{2}/
if (tomatch.test(strURL))
{
var cnt1 = strURL.length - 1;
var cnt2 = strURL.lastIndexOf(".");
if(cnt1 == cnt2 )
{
alert("Enter valid Website");

return false;
}
return true;
}
else
{ alert("Enter valid Website");
return false;
}
}
}

You can call the above function like this

<asp:TextBox ID="TextBox4" onblur="return WebsiteValidation(this.id)" runat="server"></asp:TextBox>
[/code]

No comments:

Post a Comment

Plz Share your comments...