判断字符中是否包含有特殊字符:A
function containSpecial( s ) { var containSpecial = RegExp(/[(\ )(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\')(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/); return ( containSpecial.test(s) ); }
判断字符中是否包含中文字符:B
function ischinese(s){ var regu = "^[0-9a-zA-Z]+$"; var re = new RegExp(regu); if (re.test( s )) { return false; } return true; }
最近在项目开发中,有用到以上两个js,发现他们的先后顺序对程序也是有影响的,我先前的写法是B-A,这样不论输入的是特殊字符,还是中文,js都会弹出“请不要输入中文”对话框;后来我调换了两个js的顺序,改为A-B后,这样才正确!