JavaScript验证字符串只能包含数字或者英文字符的代码实例

验证字符串只能包含数字或者英文字符的代码实例:

本章节分享一段代码实例,它实现了验证字符串内容是否只包含英文字符或者数字。

代码实例如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

functiondone(input, LengthBegin, LengthEnd) {

varpattern ='^[0-9a-zA-z]{'+ LengthBegin+','+ LengthEnd+'}$';

varregex =newRegExp(pattern);

if(input.match(regex)) {

returntrue;

}else{

returnfalse;

}

}

varone ="antzone";

vartwo ="softwhy.com888";

varthree ="蚂蚁部落softwhy";

console.log(done(one, 2,20));

console.log(done(two, 2, 10));

console.log(done(three,2, 30));