Tuesday, November 1, 2011

Valid Number

 

Valid Number Validation in JavaScript

You can use this code to validate Number and Required field validation. Put validation on Text Box. On Click of submit button this code will check whether Text Box is empty or not if Text Box is empty then it show message. It will also check the valid number when Text Box has any text in it. It will show message when number is not valid.

Insert this code between Head section of the Page.

<script language="javascript" type="text/javascript">
//<![CDATA[
   function IsValNumber(strControlName)
{

var strValue=document.getElementById(strControlName).value;
if (strValue.length > 0)
{
var rgeExp = /[^0-9]/ ;
   
if (document.getElementById(strControlName).value.search(rgeExp) >= 0) { alert("Non-numeric character(s) and space(s) are not allowed in"); return false; }
else { alert("Valid number"); return true; } } else { alert("Please enter any number"); return false; } } //]]> </script>

Insert this code between Body Tag.

<div>
<asp:TextBox ID="txtSubmit" runat="server"></asp:TextBox><asp:Button ID="btnSubmit"
runat="server" OnClientClick="javascript:return IsValNumber('txtSubmit');" Text="Submit" />
</div>

How to validate an email

 

How to validate an email address using JavaScript

You can use this code to validate Email ID and Required field validation. Put validation on Text Box. On Click of submit button this code will check whether Text Box is empty or not if Text Box is empty then it show message. It will also check the valid email Id when Text Box has any text in it.

Insert this code between Head section of the Page.

<script language="javascript" type="text/javascript">
//<![CDATA[
function IsValEmail(strControlName)
{

var stremail=document.getElementById(strControlName).value;
if (stremail.length > 0)
{
var rgeExp = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/ ;
if (document.getElementById(strControlName).value.search(rgeExp) < 0)
{
alert("Please enter valid E-mail Id");
return false;
}
else
{
alert("Valid E-mail Id");
return true;
}
}
else
{
alert("Please enter E-mail Id");
return false;
}
}
//]]>
</script>

Insert this code between Body Tag.

<div>
<
asp:TextBox ID="txtSubmit" runat="server"></asp:TextBox><asp:Button ID
="btnSubmit"
runat="server" OnClientClick="javascript:return IsValEmail('txtSubmit');" Text
="Submit" />
</
div>