Skip to content

Instantly share code, notes, and snippets.

@thosuperman
Forked from ulinkwo/usefull-validation.js
Created October 7, 2016 07:50
Show Gist options
  • Save thosuperman/338878114c24161ecaef4b88f5c8cdac to your computer and use it in GitHub Desktop.
Save thosuperman/338878114c24161ecaef4b88f5c8cdac to your computer and use it in GitHub Desktop.
Some Usefull Validations
//validate string length
function ValidLen(s,min,max){
if (s.length < min || s.length > max) { return false; }
return true;
}
// validate email
function ValidEmail(s){
var check = /^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/;
if (!check.test(s)) { return false; }
return true;
}
// validate url
function ValidUrl(s){
var check = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
if (!check.test(s)) { return false; }
return true;
}
// validate number
function ValidNumber(s){
var check = /^[0-9]\d*$/;
if (!check.test(s)) { return false; }
return true;
}
// validate ip
function ValidIp(s){
var check = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;
if (!check.test(s)) { return false; }
return true;
}
// validate date
function ValidDate(s){
var check = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
if (!check.test(s)) { return false; }
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment