Javascript validations for Numeric data and Integers

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;
}
}


Related Posts Plugin for WordPress, Blogger...

Comments

Total Views :1,627
avatar

About Moviefreak