简体   繁体   English

验证输入是否仅包含特殊字符且不带任何数字

[英]Validate if input contains only special characters without any numbers with it

Problem: I'm trying to validate when the user only inputted special characters without typing any number with it. 问题:我试图验证用户何时仅输入特殊字符而不用输入任何数字。

I'm using this expression 我正在使用这个表达

/^\+?[0-9 \.-]+$/

to accept only '+' sign, numbers, dots, hypens, and spaces when validating fax. 验证传真时仅接受“ +”号,数字,点,连字符和空格。 This is working fine. 一切正常。

But with that expression the user can input -------------- without typing any number and is accepted because it contains hypen. 但是使用该表达式,用户可以输入--------------而无需键入任何数字,并且由于包含hypen而被接受。

Question: Is there's a way to check if the input contains number? 问题:有没有一种方法可以检查输入内容是否包含数字? not just all special characters? 不仅是所有特殊字符?

UPDATE: 更新:
This is an example of valid accepted input. 这是有效接受输入的示例。

+1-2 12-98765.43 > the requirement is it should only accept '+' sign, hypen, numbers, spaces and dots. +1-2 12-98765.43 >要求是只能接受“ +”号,连字符,数字,空格和点。

Probably the easiest option is to have additional regex checks for each condition. 可能最简单的选择是针对每种情况进行其他正则表达式检查。 Eg have a regex check for just the presence of numbers /[0-9]/ and another check for just the presence of special characters /[ +.-]/ . 例如,仅对数字/[0-9]/进行正则表达式检查,而对特殊字符/[ +.-]/进行正则表达式检查。 Run these only after testing that nothing undesirable exists in the string. 仅在测试字符串中不存在任何不良现象之后才运行这些命令。

var whole = /^\+?[0-9 \.-]+$/
function validate(input) {
    // input only contains valid things
    if (!input.test(whole)) { return "Input must contain only numbers, spaces, and + . or -"; }
    // input contains each required thing
    if (!input.test(/[0-9]/)) { return "Number required"; }
    if (!input.test(/[ .-]/)) { return "Special character required"; }

    // You can also test the first character of the string with charAt()
    if (input.charAt(0) !== "+") { return "Input does not begin with +"; }
    return "Valid input";
}

I notice that your regex tests for zero or one plus, followed by a character in the list [numbers, spaces, periods, or hyphens]. 我注意到您的正则表达式会测试零或一加号,然后在列表中测试一个字符[数字,空格,句点或连字符]。 Do you mean to test for any number of pluses? 您是要测试任何数量的加号吗? The regex I've posted ( /[ +.-]/ ) should work for all the characters you want to allow. 我发布的正则表达式( /[ +.-]/ )应该适用于您要允许的所有字符。

I'm not sure this is what you're looking for, but if you want to verify that a specific single character or pattern exists in a string, you can use indexOf : 我不确定这是您要查找的内容,但是如果要验证字符串中是否存在特定的单个字符或模式,可以使用indexOf

// Require at least one hyphen
if (input.indexOf("-") === -1) { return "Please include a hyphen"; }

Update : If, as in your example, there is only one plus and it is at the beginning, then you do indeed want the \\+? 更新 :如果在您的示例中,只有一个加号,并且它在开头,那么您确实需要\\+? bit. 位。 However, you don't need to escape the period inside of square brackets. 但是,您无需在方括号内转义句号。 Supposing the plus were required, you could use charAt to test this. 假设加号是必需的,则可以使用charAt进行测试。 See updated example. 请参阅更新的示例。

This is an example of valid accepted input. 这是有效接受输入的示例。

+1-2 12-98765.43 > the requirement is it should only accept '+' sign, hypen, numbers, spaces + 1-2 12-98765.43>要求是它只能接受'+'号,连字符,数字,空格

Accepted input appear to accept . 接受的输入似乎接受. character as well ? 性格还好吗?

Try ^(\\+\\d-\\d \\d{2}-\\d+)(?:\\.\\d+|$) 尝试^(\\+\\d-\\d \\d{2}-\\d+)(?:\\.\\d+|$)

 <form> <input type="text" pattern="^(\\+\\d-\\d \\d{2}-\\d+)(?:\\.\\d+|$)" required /> <input type="submit" /> </form> 

Just add a lookahead . 只需添加lookahead

^(?=.*[0-9])\+?[0-9 \.-]+$

See demo. 参见演示。

https://regex101.com/r/eB8xU8/9 https://regex101.com/r/eB8xU8/9

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

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