Javascript validation for input fields, email, phone number and alphanumeric

web-design

// check to see if input is whitespace only or empty
function isEmpty(str) {
return (!str || 0 === str.length);
}
function isBlank(str) {
return (!str || /^\s*jQuery/.test(str));
}
// check to see if input is a valid email address
function isEmail(str) {
var regexEmail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{1,4}$/;
return (regexEmail.test(str));
}
// check to see if input has minimum length
function isMinlength(str, len) {
return (len >= str.length);
}
// check to see if input is a valid phone number
function isValidPhoneNumber(p) {
var phoneRe = /^[2-9]\d{2}[2-9]\d{2}\d{4}$/;
var digits = p.replace(/\D/g, "");
return (digits.match(phoneRe) !== null);
}
// check to see if input is number
function isNumber(val){
if (isNaN(val)) {
return false;
} else {
return true;
}
}
// check to see if input is alphabetic
function isAlphabetic(val) {
if (val.match(/^[a-zA-Z]+$/)) {
return true;
} else {
return false;
}
}

Advertisements

2 thoughts on “Javascript validation for input fields, email, phone number and alphanumeric

  1. There is problem with this method:
    function isMinlength(str) {
    return (!str || 5 >= str.length);
    }
    If string is not null, it will always return true .. because of || condition ! or i missed something?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s