These are some useful javascript functions you can use for validation in html.
You can use it
onkeyup,
onchange,
onsubmit,
onclick,
….
If only numbers need to be accepted
function allnumeric()
{
mobileNo = document.getElementById(“mobileNo”);
var numbers = /^[0-9]+$/;
if(mobileNo.value.match(numbers))
{
alert(‘Your have given a valid phone number in digits. Continue.. ‘);
return true;
}
else
{
alert(‘Please input numeric characters only’);
return false;
}
}
If numbers need not be accepted
function nonumeric()
{
username = document.getElementById(“username”);
var numbers = /^[0-9]+$/;
if(!username.value.match(numbers))
{
alert(‘Username can’t have numbers in it. Please remove it…’);
return true;
}
else
{
alert(‘Please input numeric characters only’);
return false;
}
}
If a negative or positive integer need to be checked
function integercheck()
{
integer = document.getElementById(“integer”);
var integerpattern = /^[-+]?[0-9]+$/;
if(integer.value.match(integerpattern))
{
alert(‘You haven’t given an integer. Please verify..’);
return true;
}
}
Leave a Reply