简体   繁体   English

在输入中只允许使用字母,数字和连字符?

[英]Allow only letters, numbers, and hyphen in input?

I want to accept only letters, numbers, and hyphen (dash) in the input. 我想在输入中只接受字母,数字和连字符(破折号)。 in php, i do this: 在PHP中,我这样做:

if(!preg_match("/^[A-Za-z0-9-]+$/", $input)) {
    echo "Only letters, numbers, and hyphens are allowed.";
}

how can I achieve this same thing in vanilla javascript? 如何在vanilla javascript中实现同样的功能? if its not possible in pure javascript, how can i do it in jquery? 如果它不可能在纯JavaScript中,我怎么能在jquery中做到这一点? thanks 谢谢

Adapted from Sylvain's answer here: jQuery only allow numbers,letters and hyphens . 改编自Sylvain的答案: jQuery只允许数字,字母和连字符

Note: jQuery is pretty much a wrapper for vanilla js to make a lot of common statements like Document.querySelectorAll() shorter, it's generally safe to assume that jQuery can be directly converted to vanilla js (I can't think of any examples off the top of my head where the jQuery functionality differs). 注意:jQuery几乎是vanilla js的包装器,可以使很多常见的语句如Document.querySelectorAll()更短,通常可以安全地假设jQuery可以直接转换为vanilla js(我想不出任何例子关闭我的头顶jQuery功能不同)。

What you could do is select all the inputs in a form (or whichever you would like), then test their values against a regex (Sylvain used a regex that selects the inverse of the desired characters so that it would be true if there is an invalid character) and if it has any forbidden character prevent the form from submitting and do some other things asking the user to address the issue. 你可以做的是在表单中选择所有的输入(或任何你想),然后对一个正则表达式测试他们的价值观(维尔托德使用的选择所需的字符的逆所以,这将是一个正则表达式true ,如果有一个无效字符)如果它有任何禁止字符,则阻止表单提交并做一些其他事情要求用户解决问题。

 document.getElementById("formWhitelistedChars").addEventListener("submit", function(e) { // Get all inputs in the form we specifically are looking at, this selector can be // changed if this is supposed to be applied to specific inputs var inputs = document.querySelectorAll('#formWhitelistedChars input'); var forbiddenChars = /[^az\\d\\-]/ig; // check all the inputs we selected for(var input of inputs) { // Just in case the selector decides to do something weird if(!input) continue; // Check that there aren't any forbidden chars if(forbiddenChars.test(input.value)) { // This line is just to do something on a failure for Stackoverflow // I suggest removing this and doing something different in application console.log('input ' + input.name + ' has forbidden chars.'); // Do stuff here // Prevent submit even propagation (don't submit) e.preventDefault(); return false; } } }); 
 <form id="formWhitelistedChars"> <input name="1" /> <input name="2" /> <button type="submit">Submit</button> </form> 

EDIT: Replaced 0-9 with \\d as @ibrahimmahrir mentioned in the comments 编辑:用评论中提到的@ibrahimmahrir替换0-9\\d

EDIT2: Changed input listeners to form submit EDIT2:将输入侦听器更改为表单提交

You can start a new regex using /{criteria}/{flags} . 您可以使用/{criteria}/{flags}启动新的正则表达式。 Your regex would translate to something like this: 你的正则表达式会转换成这样的东西:

/^[aA-zZ0-9-]+$/g.test(/* value to test here*/)

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

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