简体   繁体   English

javascript正则表达式,以允许字母,数字,句点,连字符,下划线和空格

[英]javascript regex to allow letters, numbers, periods, hyphens, underscores, and spaces

I'm trying to affix form data (username) to a url. 我正在尝试将表单数据(用户名)附加到URL。 On the site, a username can consist of letters, numbers, spaces, hyphens, underscores, and periods. 在网站上,用户名可以包含字母,数字,空格,连字符,下划线和句点。

I'm trying to create a javascript regex that allows those characters, but only those characters. 我正在尝试创建一个JavaScript正则表达式,以允许这些字符,但仅允许那些字符。

What I have so far will allow, for example: 到目前为止,我所允许的例如:

User Name 用户名

But it will also allow, User Name& 但这也将允许用户名和

I've searched many stackover flow posts but have not yet solved this. 我搜索了许多stackover流帖子,但尚未解决。 Thank you very much for your suggestions. 非常感谢您的建议。 Here is what I have.. 这是我所拥有的..

<script>
  function process() {
    var regexp1=new RegExp("[^[0-9A-Za-z_.-]+$]");


    var url = "http://www.website.com/page.php?data=" + document.getElementById("url").value;

    if (regexp1.test(document.getElementById("url").value)) {
      alert("Only numbers, letters, hypens, periods, spaces and underscores are allowed");
      return false;
    }
    location.href = url;
    return false;
  }
</script>

<form onSubmit="return process();">
  <br>
  <input type="text" size="10" maxlength="30" name="url" id="url">
  <input type="submit" value="go">
</form>

as far as the regex you need to anchor it with ^ and $ to make it mean "whole thing" and avoid partial mathching, also your space is outside the character class and should be in . 至于需要用^$锚定的正则表达式,使其表示“整件事情”并避免部分算术,并且您的空间也位于字符类之外,应位于 Additionally, we can get 'letters/numbers/underscore' with \\w+ , even inside a character class. 此外,即使在字符类中,也可以使用\\w+获得“字母/数字/下划线”。 Finally we can make use of the i flag to not worry about capitalized letters: 最后,我们可以使用i标志不用担心大写字母:

/^[\\w\\s.-]+$/i

https://regex101.com/r/47l22K/1 https://regex101.com/r/47l22K/1

Your regex should be : 您的正则表达式应为:

/^[ A-Za-z0-9_-.\s]*$/i

Explanation : 说明:

^   : Begging of string 
A-Z : Uppercase Characters 
a-z : Lowercase Characters 
0-9 : Numbers 
_-. : Special Characters  you requested
\s  : Spaces 
*   : Allow repeat
$   : End of string 
/i  : Case insensitive 

You can replace A-Za-z0-9_ with \\w 您可以用\\w替换A-Za-z0-9_
And your If stament should check for the inverse : 而您的If步调应该检查逆:

if(!regexp1.test...

And at the end of the function it's better to make it 在功能的最后,最好将其制成

return true;

I suggest you check JQuery for more advanced, easier Javascript code 我建议您检查JQuery以获得更高级,更简单的Javascript代码
Hope this help 希望这个帮助

Your if statement is reversed. 您的if语句被颠倒了。 You should check when the regex DOES NOT match instead: 您应该检查正则表达式何时不匹配:

!regexp1.test(document.getElementById("url").value)

Also I believe the original regex is wrong/inaccurate try the one shown below: 我也相信原始的正则表达式是错误的/不正确的,请尝试如下所示:

 function process() { var regexp1=new RegExp("^[0-9A-Za-z_.-]+$"); var url = "http://www.website.com/page.php?data=" + document.getElementById("url").value; if (!regexp1.test(document.getElementById("url").value)) { console.log("Only numbers, letters, hypens, periods, spaces and underscores are allowed"); } else { console.log("Passed validation."); } } 
 <input type="text" size="10" maxlength="30" name="url" id="url"> <input type="button" onclick="process()"> 

暂无
暂无

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

相关问题 正则表达式:仅限字母、数字、空格、连字符、句点和下划线,并且必须以字母开头 - Regular Expression: Letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter javascript 和 php 正则表达式只允许字母、数字、句号、星号、下划线和破折号 - javascript and php regex to allow only letters, numbers, full stops, asterisks, underscores and dashes 正则表达式允许字母数字和空格,但不允许连续两个空格 - RegEx to allow letters numbers and spaces but not two spaces in a row 正则表达式(连字符,下划线和句点) - Regular Expression (Hyphens, Underscores, and Periods) javascript:使用RegEx允许字母,数字和空格(至少包含一个字母和数字) - javascript: Use RegEx to allow letters, numbers, and spaces (with at least one letter and number) jQuery只允许数字,字母和连字符 - jQuery only allow numbers,letters and hyphens JavaScript正则表达式-数字之间的连字符 - JavaScript regex - hyphens between numbers 如何在 javascript 中创建仅允许字母、数字、破折号 -、下划线 _ 和空格的 function? - How do I create a function in javascript that only allows letters, numbers, dashes -, underscores _ and spaces? RegEx for Javascript(替换功能)仅允许使用字母和空格,而不能以空格开头? - RegEx for Javascript (replace function) to allow only letters and spaces, but not start with space? javascript 使用正则表达式测试字母、数字、空格和破折号 - javascript use test with regex for letters, numbers, spaces and dash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM