验证字符串只能包含数字或者英文字符的代码实例:
本章节分享一段代码实例,它实现了验证字符串内容是否只包含英文字符或者数字。
代码实例如下:
- function done(input, LengthBegin, LengthEnd) {
- var pattern = '^[0-9a-zA-z]{' + LengthBegin+ ',' + LengthEnd+ '}$';
- var regex = new RegExp(pattern);
- if (input.match(regex)) {
- return true;
- } else {
- return false;
- }
- }
- var one = "antzone";
- var two = "softwhy.com888";
- var three = "蚂蚁部落softwhy";
- console.log(done(one, 2,20));
- console.log(done(two, 2, 10));
- console.log(done(three,2, 30));
来源: http://www.phpxs.com/code/1009833/