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>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.