Javascript onlyNumber Custom Validation at client Side

Jayeshl
Posted by Jayeshl under ASP.NET category on | Points: 40 | Views : 2858
Javascript onlyNumber Custom Validation at client Side

function NumberOnly(eve)
{
try {
var val1 = (eve.which) ? eve.which : event.keycode
if (val1 > 31 && (val1 < 48) || (val1 > 57)) {
return false; }
}
catch (err) {
alert(err);
}
}

// HTML Example

<table cellpadding="4" cellspacing="2" border="0" width="100%">
<tr>
<td align="right" width="10%">
<asp:Label ID="lblNumber" runat="server" Text="Number"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtnumber" onKeyPress="javascript:return NumberOnly(event);" runat="server" ></asp:TextBox>
</td>
</tr>
</table>

Jayesh Lolariya
http://sqlassistant.blogspot.com

Comments or Responses

Posted by: Ndebata on: 7/24/2011 Level:Starter | Status: [Member] | Points: 10
But you are not stopping users to Paste or Drag a non number text to this textbox.
Posted by: Jayeshl on: 7/24/2011 Level:Starter | Status: [Member] | Points: 10
ya you are right but in case of "stop paste" on text we have html attribute for restrict user to past any data on textbox.

<INPUT size=12 name="txtnumber" onCopy="return false" onDrag="return false" onDrop="return false" onPaste="return false" autocomplete=off >

thanks
best regards
Jayesh L
http://sqlassistant.blogspot.com

Login to post response