Created
April 11, 2011 16:57
-
-
Save lsm/913855 to your computer and use it in GitHub Desktop.
email,nickname,mobile,password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RegExp copied from sina weibo;) | |
function makeReFn(regStr) { | |
var re = typeof regStr == 'string' ? new RegExp(regStr) : regStr; | |
return function (beRegStr) { | |
if (re.test(beRegStr)) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
} | |
function byteLength(str) { | |
if (!str) { | |
return 0; | |
} | |
var aMatch = str.match(/[^\x00-\xff]/g); | |
return (str.length + (!aMatch ? 0 : aMatch.length)); | |
} | |
// ASCII, chinese, wihtespace, '-' | |
var reNick = makeReFn(/^[0-9a-zA-Z_\u4e00-\u9fa5\s-]*$/), | |
reMobile = makeReFn("^1(\\d{10})+$"), | |
rePassword = makeReFn("^([\\w\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\+\\`\\-\\=\\[\\]\\\\{\\}\\|\\;\\'\\:\\\"\\,\\.\\/\\<\\>\\?]{6,16})$"); | |
var Validator = function(trim) { | |
this.trim = trim === undefined ? true : trim; | |
}; | |
Validator.prototype = { | |
reNick: reNick, | |
// byte length between 4 and 20, any ASCII, chinese, wihtespace(not leading/ending), '-' except all numbers | |
// not leading with '_', '-', ' ' | |
nick: function(nick) { | |
if (typeof nick != 'string') { | |
return false; | |
} | |
this.trim && (nick = nick.trim()); | |
var len = byteLength(nick); | |
if (len < 4 || len > 20) { | |
return false; | |
} | |
if (!this.reNick(nick)) { | |
return false; | |
} | |
if (/^[0-9]*$/.test(nick)) { | |
return false; | |
} | |
if (/^[_-]/.test(nick)) { | |
return false; | |
} | |
return nick; | |
}, | |
email: function (eml) { | |
if (typeof eml != 'string') { | |
return false; | |
} | |
this.trim && (eml = eml.trim()); | |
if (!/^[\.\w]([(\/)(\-)(\+).\w])*@([(\-)\w]{1,64}\.){1,7}[(\-)\w]{1,64}$/.test(eml)) { | |
return false; | |
} else { | |
if (eml && eml != "" && (eml.indexOf("@") != -1)) { | |
var indexOfA = eml.indexOf("@"), | |
name = eml.substring(0, indexOfA); | |
if (name.length > 64 || eml.length > 254) { | |
return false; | |
} else { | |
return eml.toLowerCase(); | |
} | |
} | |
} | |
return false; | |
}, | |
reMobile: reMobile, | |
mobile: function(mob) { | |
if (typeof mob != 'string') { | |
return false; | |
} | |
this.trim && (mob = mob.trim()); | |
return this.reMobile(mob) ? mob : false; | |
}, | |
rePassword: rePassword, | |
password: function(pass) { | |
if (typeof pass != 'string') { | |
return false; | |
} | |
return this.rePassword(pass) ? pass : false; | |
} | |
}; | |
try { | |
if (module && module.exports) { | |
// for nodejs | |
module.exports = Validator; | |
} | |
} catch(e) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment