以前一直没有写过javascript,这次做客户端验证,使用了其他人提供的validator.js
http://www.blogjava.net/Files/wldandan/validate.js这片文章主要备忘一下,方便自己以后使用。
该JS主要代码如下:
hasValidChars:function(s, characters, caseSensitive){
function escapeSpecials(s){
return s.replace(new RegExp("([\\\\-])", "g"), "\\$1");
}
return new RegExp("^[" + escapeSpecials(characters) + "]+$",(!caseSensitive ? "i" : "")).test(s);
},
/*--------------------------------------------------------------------------*/
isSimpleIP:function(ip){
ipRegExp = /^(([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+))$/
return ipRegExp.test(ip);
},
/*--------------------------------------------------------------------------*/
isAlphaLatin:function(string){
alphaRegExp = /^[0-9a-z]+$/i
return alphaRegExp.test(string);
},
/*--------------------------------------------------------------------------*/
isNotEmpty:function (string){
return /\S/.test(string);
},
/*--------------------------------------------------------------------------*/
isEmpty:function(s){
return !/\S/.test(s);
},
/*--------------------------------------------------------------------------*/
isInteger:function(n){
numberExp = /^-?\d+$/
return numberExp.test(n);
},
/*--------------------------------------------------------------------------*/
isInteger:function(n){
numberExp = /^-?\d+$/
return numberExp.test(n);
},
isPositiveInteger:function(n){
regex = /^[0-9]*[1-9][0-9]*$/
return regex.test(n);
},
isNonPositiveInteger:function(n){
regex = /^((-\d+)|(0+))$/
return regex.test(n);
},
isNonNegativeInteger:function(n){
//var regex = /^[1-9]\d*|0$/; error
var regex = /^((\d+)|(0+))$/;
return regex.test(n);
},
/*--------------------------------------------------------------------------*/
isIntegerInRange:function(n,Nmin,Nmax){
var num = Number(n);
if(isNaN(num)){
return false;
}
if(num != Math.round(num)){
return false;
}
return (num >= Nmin && num <= Nmax);
},
/*--------------------------------------------------------------------------*/
isNum:function(number){
numRegExp = /^[0-9]+$/
return numRegExp.test(number);
},
/*--------------------------------------------------------------------------*/
isEMailAddr:function(string){
emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/
return emailRegExp.test(string);
},
/*--------------------------------------------------------------------------*/
isZipCode:function(zipcode,country){
if(!zipcode) return false;
if(!country) format = 'US';
switch(country){
case'US': zpcRegExp = /^\d{5}$|^\d{5}-\d{4}$/; break;
case'MA': zpcRegExp = /^\d{5}$/; break;
case'CA': zpcRegExp = /^[A-Z]\d[A-Z] \d[A-Z]\d$/; break;
case'DU': zpcRegExp = /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/; break;
case'FR': zpcRegExp = /^\d{5}$/; break;
case'Monaco':zpcRegExp = /^(MC-)\d{5}$/; break;
}
return zpcRegExp.test(zipcode);
},
/*--------------------------------------------------------------------------*/
isDate:function(date,format){
if(!date) return false;
if(!format) format = 'FR';
switch(format){
case'FR': RegExpformat = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/([2][0]|[1][9])\d{2})$/; break;
case'US': RegExpformat = /^([2][0]|[1][9])\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/; break;
case'SHORTFR': RegExpformat = /^([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/\d{2}$/; break;
case'SHORTUS': RegExpformat = /^\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/; break;
case'dd MMM yyyy':RegExpformat = /^([0-2]\d|[3][0-1])\s(Jan(vier)?|Fév(rier)?|Mars|Avr(il)?|Mai|Juin|Juil(let)?|Aout|Sep(tembre)?|Oct(obre)?|Nov(ember)?|Dec(embre)?)\s([2][0]|[1][19])\d{2}$/; break;
case'MMM dd, yyyy':RegExpformat = /^(J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugust)|(((Sept|Nov|Dec)em)|Octo)ber)\s([0-2]\d|[3][0-1])\,\s([2][0]|[1][9])\d{2}$/; break;
}
return RegExpformat.test(date);
},
isDateTime:function(date){
//TODO: validate the number of year and month
var regex = /^(\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/;
return regex.test(date);
},
isDuration:function(n){
var regex = /^[-]?P(?!$)(?:\d+Y)?(?:\d+M)?(?:\d+D)?(?:T(?!$)(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?$/
return regex.test(n);
},
isRestrictDuration:function(n){
var regex = /^[-]?PT(?!$)(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?$/;
return regex.test(n);
},
isUnsignedLong:function(num){
var maxValue = "18446744073709551615";
var maxLength = maxValue.length;
var r = true;
if(!this.isNonNegativeInteger(num)){
r = false;
}else{
if(num.length > maxLength){
r = false;
}else{
if(num.length == maxLength
&& (num.charAt(maxLength-1)>5)){
r = false;
}
}
}
return r;
//return this.isIntegerInRange(num , 0 , 18446744073709551615);
},
isPrintableString:function(_s , _min , _max){
if(_s.length < _min || _s.length > _max){
return false;
}
for (var i = 0; i < _s.length; i++) {
var ch = _s.charCodeAt(i);
if(((ch < 32 && ch != 10 && ch != 13 && ch !=9 ) || ch > 126)){
return false;
}
}
return true;
},
/*--------------------------------------------------------------------------*/
isMD5:function(string){
if(!string) return false;
md5RegExp = /^[a-f0-9]{32}$/;
return md5RegExp.test(string);
},
/*--------------------------------------------------------------------------*/
isURL:function(string){
if(!string) return false;
string = string.toLowerCase();
urlRegExp = /^\s*(((ht|f)tp(s?))\:\/\/)([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?\s*$/
//urlRegExp = /^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)\://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?/
return urlRegExp.test(string);
},
/*--------------------------------------------------------------------------*/
isGuid:function(guid){//guid format : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx or xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
if(!guid) return false;
GuidRegExp = /^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$/
return GuidRegExp.test(guid);
},
/*--------------------------------------------------------------------------*/
isISBN:function(number){
if(!number) return false;
ISBNRegExp = /ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$/
return ISBNRegExp.test(number);
},
/*--------------------------------------------------------------------------*/
isSSN:function(number){//Social Security Number format : NNN-NN-NNNN
if(!number) return false;
ssnRegExp = /^\d{3}-\d{2}-\d{4}$/
return ssnRegExp.test(number);
},
/*--------------------------------------------------------------------------*/
isDecimal:function(number){// positive or negative decimal
if(!number) return false;
decimalRegExp = /^-?(0|[1-9]{1}\d{0,})(\.(\d{1}\d{0,}))?$/
return decimalRegExp.test(number);
},
/*--------------------------------------------------------------------------*/
isplatform:function(platform){
//win, mac, nix
if(!platform) return false;
var os;
winRegExp = /\win/i
if(winRegExp.test(window.navigator.platform)) os = 'win';
macRegExp = /\mac/i
if(macRegExp.test(window.navigator.platform)) os = 'mac';
nixRegExp = /\unix|\linux|\sun/i
if(nixRegExp.test(window.navigator.platform)) os = 'nix';
if(platform == os) return true;
else return false;
},
isIe:function(){
var r = false;
var agent = navigator.userAgent;
var MSIE = agent.indexOf("MSIE");
var NETS = agent.indexOf("Netscape");
var OPER = agent.indexOf("Opera");
if(MSIE == 25) r = true;
return r;
},
isHex:function(date){
var regex =/^([0-9a-fA-F])*$/;
return regex.test(date);
},
使用如下:
function validate(){
var form = document.forms[0];
var _validate = new Validate();
var m = document.getElementById(xxx);
if (_validate.isEmpty(m.value)){
alert(xxx);
return false;
}
m = document.getElementById(xxx);
if (!_validate.isEmpty(m.value)){
if ( !_validate.isPrintableString(m.value,1,100)){
alert(xxx);
return false;
}
}
}