Sep 22, 2009

Javascript validation for numeric or decimal value

How to make a textbox wont even allow user to type a invalid text :

// To check whether the value entered is a numeric or decimal value.

// This method will wont even allow the user to type the invalid value.

function isNumber(field) {

var re = /^[0-9.]*$/;

if (!re.test(field.value)) {

field.value = field.value.replace(/[^0-9.]/g, "");


The above method will wont even allow you to type any text or * or $ symbol in textbox.
// To make a call to the method on onkeyup event.

<asp:TextBox ID="txtRadNZoomValue" Runat="server" Width="65px" ForeColor="Black" onkeyup="isNumber(this)">

This is also tested in master pages scenerio .

No comments: