Question:
numeric validation using java script?
Satheeshkumar
2011-12-12 00:56:05 UTC
i need a code using java script to validate a field, that should accept only numbers..
can you please help me with your code
Three answers:
Kratika
2011-12-12 01:07:50 UTC


Such a function can be used as follows to check a field and return an error message if the contents are not numeric:

if (document.frmUser.afield.value.length == 0)

{

alert("Please enter a value.");

}

else if (chkNumeric(document.frmUser.afield.value) == false)

{

alert("Please check - non numeric value!");

}
Christopher
2011-12-12 09:32:04 UTC
Here's how I would do it, it checks the user input if it's a digit, if not then it'll remove that character:



function isNumeric(id) {

var numPatt = /\D/;

var userInput = document.getElementById(id).value;



if(userInput.match(numPatt)) {

correctUserInput = userInput.substr(0, userInput.length-1);

document.getElementById(id).value = correctUserInput;

}

} // end of isNumeric





Hope this helps :)
engineergundam
2011-12-12 09:06:19 UTC
http://www.javascript-coder.com/html-form/javascript-form-validation.phtml



Is this what your looking for? sheeesh just entered the searched engine and it was the third address from urs.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...