admin管理员组文章数量:1356560
I have found this function to check if a string only contains alphanumeric characters:
function test ( value ) {
if( value.match("^[a-zA-Z0-9]*$")){
alert('ok');
}
}
test( 'Somevalue123' );
Now I would like to improve the above function to allow some other "special characters" like:
(empty space)
' (single quote)
- (hyphen)
èàùìòÈÀÒÙÌéáúíóÉÁÚÍÓëäüïöËÄÜÏÖêâûîôÊÂÛÎÔç
And some symbols, like:
'_', '&', '.', ',', ':', '(', ')', '[', ']', '/', '+', '=', '?', '#', '@'
So I have try to update it and I was able to do so:
function test ( value, numbers, symbols ) {
numbers = ( typeof numbers === 'undefined' ) ? 0 : 1; // default numbers not allowed
symbols = ( typeof symbols === 'undefined' ) ? 0 : 1; // default symbols not allowed
value = value.replace(/è|à|ù|ì|ò|È|À|Ò|Ù|Ì|é|á|ú|í|ó|É|Á|Ú|Í|Ó|ë|ä|ü|ï|ö|Ë|Ä|Ü|Ï|Ö|ê|â|û|î|ô|Ê|Â|Û|Î|Ô|ç/g, '' ); // remove allowed characters before check
// value = value.replace(/...???.../g, '' ); allow white space, single quote and symbols?
if( numbers == 1 && value.match("^[a-zA-Z0-9 ]*$")){ alert('ok'); } // number allowed
else if( value.match("^[a-zA-Z ]*$")){ alert('ok'); } // number not allowed
}
Not sure how to allow white space, single quote and symbols (with Regex or value.replace() is the same for me).
I have found this function to check if a string only contains alphanumeric characters:
function test ( value ) {
if( value.match("^[a-zA-Z0-9]*$")){
alert('ok');
}
}
test( 'Somevalue123' );
Now I would like to improve the above function to allow some other "special characters" like:
(empty space)
' (single quote)
- (hyphen)
èàùìòÈÀÒÙÌéáúíóÉÁÚÍÓëäüïöËÄÜÏÖêâûîôÊÂÛÎÔç
And some symbols, like:
'_', '&', '.', ',', ':', '(', ')', '[', ']', '/', '+', '=', '?', '#', '@'
So I have try to update it and I was able to do so:
function test ( value, numbers, symbols ) {
numbers = ( typeof numbers === 'undefined' ) ? 0 : 1; // default numbers not allowed
symbols = ( typeof symbols === 'undefined' ) ? 0 : 1; // default symbols not allowed
value = value.replace(/è|à|ù|ì|ò|È|À|Ò|Ù|Ì|é|á|ú|í|ó|É|Á|Ú|Í|Ó|ë|ä|ü|ï|ö|Ë|Ä|Ü|Ï|Ö|ê|â|û|î|ô|Ê|Â|Û|Î|Ô|ç/g, '' ); // remove allowed characters before check
// value = value.replace(/...???.../g, '' ); allow white space, single quote and symbols?
if( numbers == 1 && value.match("^[a-zA-Z0-9 ]*$")){ alert('ok'); } // number allowed
else if( value.match("^[a-zA-Z ]*$")){ alert('ok'); } // number not allowed
}
Not sure how to allow white space, single quote and symbols (with Regex or value.replace() is the same for me).
Share Improve this question asked Jun 9, 2016 at 10:28 ipelipel 1,3461 gold badge18 silver badges46 bronze badges 2- 2 Just add all these characters in the character class. – Toto Commented Jun 9, 2016 at 10:29
- for empty space use \s , signle quote use \' , hypen \- and all other characters you can include in character class mean [ and ] as @Toto said. – Shekhar Khairnar Commented Jun 9, 2016 at 10:31
2 Answers
Reset to default 4Use character class and add all the whitelisted characters in the character class.
Make sure the hyphen -
is escaped by preceding it with backslash \-
or added at the beginning or end in the character class.
/^[a-zA-Z0-9 èàùìòÈÀÒÙÌéáúíóÉÁÚÍÓëäüïöËÄÜÏÖêâûîôÊÂÛÎÔç'-]*$/
Also, use RegExp#test
instead of String#match
function test(value) {
if (/^[a-zA-Z0-9 èàùìòÈÀÒÙÌéáúíóÉÁÚÍÓëäüïöËÄÜÏÖêâûîôÊÂÛÎÔç'-]*$/.test(value)) {
alert('Ok');
}
}
You can try something like this:
/^[a-z0-9-\'_\.,:\(\)&\[\]\/+=\?#@ \xC0-\xFF]+$/i
Logic
/a-z0-9/i
will handle alphanumeric characters\xC0-\xFF
will handle foreign character- For symbols, you can list them individually.
An alternate could be listing all characters that are not allowed and use negate regex like
/^[^!~`\>\<]$/gi
本文标签:
版权声明:本文标题:regex - javascript check if string contains only alphanumeric characters + other special characters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743996113a2573019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论