[英]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.