简体   繁体   English

Java脚本输入格式验证

[英]Java Script Input format validation

I wanted to validate the below format of one of the input fields 我想验证以下输入字段之一的格式

1111-111-111 //4digits- 3digits- 3digits

I tried the below method but its not working. 我尝试了以下方法,但无法正常工作。 Any idea of what am I misisng? 我误会了什么吗?

function(value) {
    var patt = "/^\d{4}-d{3}-d{3}$/";
    var res = value.match(patt);
    if (!res) {
        return {
            status: 2,
            message: 'Input should be formatted as 0000-000-000'
        };
    } else {
        return {
            status: 0
        };
    }
}

Also, given a string, I wanted to enforce characters at certain indices are empty. 另外,给定一个字符串,我想强制某些索引为空的字符。 like for eg: 23 total characters, where 2nd, 9th, 11th and 13-21 are space characters. 例如:总共23个字符,其中第2、9、11和13-21是空格字符。 I am new to Javascript regex, how do we enforce these? 我是Java regex的新手,我们如何执行这些规则?

Your regular expression is wrong. 您的正则表达式是错误的。 Fix it: 修理它:

/^\d{4}-\d{3}-\d{3}$/

Your second part and third part are "d" characters, not numbers. 您的第二部分和第三部分是“ d”字符,而不是数字。 And remove the " characters when declare a regex. 并在声明正则表达式时删除"字符。

var patt = /^\d{4}-\d{3}-\d{3}$/;

If we only wish to pass 4-3-3 digits, we could simply do that using: 如果我们只希望传递4-3-3位数字,我们可以简单地使用以下方法:

^[0-9]{4}-[0-9]{3}-[0-9]{3}$

Demo 演示

or: 要么:

^\d{4}-\d{3}-\d{3}$

Demo 演示

If we wish to also capture the phone number, we would be adding a capturing group: 如果我们也希望捕获电话号码,我们将添加一个捕获组:

^([0-9]{4}-[0-9]{3}-[0-9]{3})$

 const regex = /^[0-9]{4}-[0-9]{3}-[0-9]{3}$/gm; const str = `1111-111-111 1111-111-1111 8888-888-888`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); } 

RegEx Circuit RegEx电路

jex.im visualizes regular expressions: jex.im可视化正则表达式:

在此处输入图片说明

The first pattern does not work like intended because to match a digit you have to escape the d like \\d or else d{3} would match 3 times a d character and the pattern should not be between double quotes but between / ... / 第一种模式无法按预期工作,因为要匹配一个数字,您必须像\\d d一样转义\\d ,否则d{3}将匹配d字符的3倍,并且该模式不应在双引号之间,而应在/之间/

 let test = function(value) { var patt = /^\\d{4}-\\d{3}-\\d{3}$/; var res = value.match(patt); if (!res) { return { status: 2, message: 'Input should be formatted as 0000-000-000' }; } else { return { status: 0 }; } }; console.log(test("1111-111-111")); 

For matching spaces at the places 2nd, 9th, 11th and 13-21 in the string you could use quantifiers and \\S to match a non whitespace character: 要在字符串的第2、9、11和13-21位匹配空格,可以使用量词\\S来匹配非空白字符:

^\S \S{6} \S \S {11}$

Regex demo 正则表达式演示

If you want to match by index based on a zero index, the pattern would look like: 如果要基于零索引进行索引匹配,则模式如下所示:

^\S\S \S{6} \S \S {10}$

Regex demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM