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

时间:2022-11-21 16:51:57

I'm trying to affix form data (username) to a url. On the site, a username can consist of letters, numbers, spaces, hyphens, underscores, and periods.

我试图将表单数据(用户名)附加到url。在这个网站上,用户名可以由字母、数字、空格、连字符、下划线和句点组成。

I'm trying to create a javascript regex that allows those characters, but only those characters.

我正在创建一个javascript regex,它允许这些字符,但只允许这些字符。

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. 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>

3 个解决方案

#1


0  

Your if statement is reversed. You should check when the regex DOES NOT match instead:

if语句被颠倒了。当regex不匹配时,您应该检查:

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

Also I believe the original regex is wrong/inaccurate try the one shown below:

我也相信原来的regex是错误的/不准确的尝试如下所示:

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()">

#2


2  

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. Finally we can make use of the i flag to not worry about capitalized letters:

到正则表达式需要锚^和$使它的意思是“整体”,避免部分系统,还你的空间以外的字符类,应该的。此外,我们还可以使用\w+获得'字母/数字/下划线',甚至在字符类中也可以。最后,我们可以利用i标志,不用担心大写字母:

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

/ ^[\ w \ s -]+ $ /我

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

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

#3


1  

Your regex should be :

您的regex应该是:

/^[ 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
And your If stament should check for the inverse :

你可以将A-Za-z0-9_替换为\w,你的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
Hope this help

我建议您检查JQuery以获得更高级的、更简单的Javascript代码。

#1


0  

Your if statement is reversed. You should check when the regex DOES NOT match instead:

if语句被颠倒了。当regex不匹配时,您应该检查:

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

Also I believe the original regex is wrong/inaccurate try the one shown below:

我也相信原来的regex是错误的/不准确的尝试如下所示:

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()">

#2


2  

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. Finally we can make use of the i flag to not worry about capitalized letters:

到正则表达式需要锚^和$使它的意思是“整体”,避免部分系统,还你的空间以外的字符类,应该的。此外,我们还可以使用\w+获得'字母/数字/下划线',甚至在字符类中也可以。最后,我们可以利用i标志,不用担心大写字母:

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

/ ^[\ w \ s -]+ $ /我

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

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

#3


1  

Your regex should be :

您的regex应该是:

/^[ 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
And your If stament should check for the inverse :

你可以将A-Za-z0-9_替换为\w,你的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
Hope this help

我建议您检查JQuery以获得更高级的、更简单的Javascript代码。