Click to See Complete Forum and Search --> : number validation


pap
05-03-2007, 07:50 AM
hi,

this is the doubt regarding java script validation.
how to check the data which is entered by the user contains only numeric values. it should not contain any specioal characters,or alphabetics,or spaces etc.. it should contain only digits.how to do validation for this using java script.. can anybody sebd me the code for this.. it's urgent plz...

thanks in advance,
pap.

Typhoon101
05-03-2007, 08:09 AM
Try this.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function validate(frm){
digitCheck = /^\d+$/;
if(digitCheck.test(frm.textBox.value)){
alert("contains only numbers")
} else {
alert("contains other characters")
}

return digitCheck.test(frm.textBox.value)
}
</script>
</head>
<body>
<form onSubmit="return validate(this)">
<input type="text" name="textBox" id="textBox" />
<input type="submit" value="submit" />
</form>
</body>
</html>

felgall
05-03-2007, 01:43 PM
Easier to just use:

if (frm.textBox.value == +frm.textBox.value) {// is a number