admin管理员组文章数量:1130349
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.
I'm trying to create a javascript regex that allows those characters, but only those characters.
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 = ".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>
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.
I'm trying to create a javascript regex that allows those characters, but only those characters.
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./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>
Share
Improve this question
edited Jan 18, 2017 at 0:19
ryansd
asked Jan 17, 2017 at 22:22
ryansdryansd
431 gold badge1 silver badge5 bronze badges
3 Answers
Reset to default 5Your if statement is reversed. 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./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()">
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
And your If stament should check for the inverse :
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
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\s.-]+$/i
https://regex101./r/47l22K/1
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.
I'm trying to create a javascript regex that allows those characters, but only those characters.
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 = ".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>
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.
I'm trying to create a javascript regex that allows those characters, but only those characters.
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./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>
Share
Improve this question
edited Jan 18, 2017 at 0:19
ryansd
asked Jan 17, 2017 at 22:22
ryansdryansd
431 gold badge1 silver badge5 bronze badges
3 Answers
Reset to default 5Your if statement is reversed. 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./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()">
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
And your If stament should check for the inverse :
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
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\s.-]+$/i
https://regex101./r/47l22K/1
本文标签:
版权声明:本文标题:javascript regex to allow letters, numbers, periods, hyphens, underscores, and spaces - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1740786364a1775641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论