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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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));

 

转载于:https://www.cnblogs.com/telwanggs/p/5305494.html

你可能感兴趣的:(JavaScript验证字符串只能包含数字或者英文字符的代码实例)