字符串的字段验证以及 JavaScript 中的数字
field validation for string along with number in javascript
我有一个密码字段。现在我必须以这样一种方式进行验证,即字段值应该是 7 位字符串和一个数字。否则它将返回 false。
请帮助我。
Create regex first
Var regex = /'w{7}'d/i;
var yourvalue=$("#passwordid").value;
regex.test(yourvalue){
return true;
}
else{
return false
}
我相信有更好的方法,但是像这样:
if ( /.{7}/.test(str) && /'d/.test(str) ) {
//OK
}
在你的 JavaScript 中,你可以使用 RegExp 对象。
var regEx = new RegExp(pattern, modifiers);
或者更简单地说:
var pattern = /pattern/modifiers;
例如
var password = "abcdefg1";
var pattern = /'w{7}'d/i;
var isMatch = pattern.test(password);
以下是一些表达式:
[abc] Find any character between the brackets
[^abc] Find any character not between the brackets
[0-9] Find any digit from 0 to 9
[A-Z] Find any character from uppercase A to uppercase Z
[a-z] Find any character from lowercase a to lowercase z
[A-z] Find any character from uppercase A to lowercase z
[adgk] Find any character in the given set
[^adgk] Find any character outside the given set
(red|blue|green) Find any of the alternatives specified
元字符:
. Find a single character, except newline or line terminator
'w Find a word character
'W Find a non-word character
'd Find a digit
'D Find a non-digit character
's Find a whitespace character
'S Find a non-whitespace character
'b Find a match at the beginning/end of a word
'B Find a match not at the beginning/end of a word
'0 Find a NUL character
'n Find a new line character
'f Find a form feed character
'r Find a carriage return character
't Find a tab character
'v Find a vertical tab character
'xxx Find the character specified by an octal number xxx
'xdd Find the character specified by a hexadecimal number dd
'uxxxx Find the Unicode character specified by a hexadecimal number xxxx
量词
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{X} Matches any string that contains a sequence of X n's
n{X,Y} Matches any string that contains a sequence of X to Y n's
n{X,} Matches any string that contains a sequence of at least X n's
n$ Matches any string with n at the end of it
^n Matches any string with n at the beginning of it
?=n Matches any string that is followed by a specific string n
?!n
相关文章:
- RegEx JavaScript:数字后的符号提取
- HTML文本框中的javascript数字
- Javascript数字选择器函数
- 我无法让我的javascript数字分类器工作
- 以零结尾的Javascript数字
- Javascript数字放置和如何,4 = 0.04,14 = 0.14,100 = 1.00
- JavaScript 数字近似
- Javascript数字验证功能起作用+需要建设性的批评
- 在小数点后给出一个固定宽度的 JavaScript 数字,同时保留一个数字
- Javascript 数字表示
- Javascript数字变量比较不起作用
- JavaScript 数字继承
- 如何将包含科学记数法的字符串转换为正确的 Javascript 数字格式
- JavaScript:数字计数器实时更新
- Javascript数字到单词 - vigesimal
- Javascript数字到单词 - vigesimal 200-999
- 将 JavaScript 数字转换为小数点后两位
- 带有输入模式的 Javascript 数字和逗号
- 我如何让这个 JavaScript 数字猜谜游戏在没有“while”或“for”循环的情况下工作
- Javascript数字辅助