| | |
Regex to remove 'Illegal Characters'
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi all
I've been trying to find a regular expression that checks if input contains any of the following characters only:
`~!@#$%^&*()-=+\|/?.>,<;:'"[{]}
I want to allow users to input any normal character a-z or any numbers as well as underscores and any special character that resembles a letter such as é, ê, ô or ÿ etc
So far I have the following which doesn't allow for any of the special characters that I want to allow users to use:
The code i'm using is as follows:
I've been trying to find a regular expression that checks if input contains any of the following characters only:
`~!@#$%^&*()-=+\|/?.>,<;:'"[{]}
I want to allow users to input any normal character a-z or any numbers as well as underscores and any special character that resembles a letter such as é, ê, ô or ÿ etc
So far I have the following which doesn't allow for any of the special characters that I want to allow users to use:
javascript Syntax (Toggle Plain Text)
/[^\w\s]/g
javascript Syntax (Toggle Plain Text)
function checkName (strng) { var error = ""; var illegalChars = /[^\w\s]/g; // allow letters, numbers, and underscores if (strng == "") { error = "Please enter your name.\n"; } else if((strng.length < 2)) { error = "The name is the wrong length.\n"; } else if (illegalChars.test(strng)) { error = "The name contains illegal characters.\n"; } return error; }
This user has a spatula. We don't know why, but we are afraid.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
0
#2 34 Days Ago
Try the following character class:
There is a fun website at http://hamstersoup.com/javascript/re...ss_tester.html that gives you the unicode expressions for any character class you specify.
var illegalChars = /[\u0021-\u002f\u003a-\u0040\u005b-\u005e\u0060\u007b-\u007e]/g; // Don't allow any of these Javascript supports specifying unicode characters by hexadecimal expressions like \u0060 and ranges like \u007b-\u007e .There is a fun website at http://hamstersoup.com/javascript/re...ss_tester.html that gives you the unicode expressions for any character class you specify.
0
#3 33 Days Ago
•
•
•
•
Try the following character class:
var illegalChars = /[\u0021-\u002f\u003a-\u0040\u005b-\u005e\u0060\u007b-\u007e]/g; // Don't allow any of theseJavascript supports specifying unicode characters by hexadecimal expressions like\u0060and ranges like\u007b-\u007e.
There is a fun website at http://hamstersoup.com/javascript/re...ss_tester.html that gives you the unicode expressions for any character class you specify.
This user has a spatula. We don't know why, but we are afraid.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
0
#4 32 Days Ago
•
•
•
•
...the 'illegal' characters are only detected if they are the first or last letter. If it's anywhere between valid characters it goes undetected.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Test regex punctuation filter</title> <script type="text/javascript"> function checkName (strng) { var error = ""; var illegalChars = /[\u0021-\u002f\u003a-\u0040\u005b-\u005e\u0060\u007b-\u007e]/g; // Don't allow any of these if (strng == "") { error = "Please enter your name.\n"; } else if((strng.length < 2)) { error = "The name is the wrong length.\n"; } else if (illegalChars.test(strng)) { error = "The name contains illegal characters.\n"; } return error; } // The following lines test the function with a string containing illegal chars. var teststring = "Here is a string with ill:egal ch@racters"; var errmsg = checkName(teststring); //alert("Testing \'" + teststring + "\' results in \'" + errmsg + "\'"); document.writeln("Testing <p><b>" + teststring + "</b><p> results in <p><b>" + errmsg); </script> </head> <body> </body> </html>
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
I simplified my test script. It still seems to find illegal characters wherever they are in the test string.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Test regex punctuation filter</title> <script type="text/javascript"> function checkString (strng) { var error = false; var illegalChars = /[\u0021-\u002f\u003a-\u0040\u005b-\u005e\u0060\u007b-\u007e]/g; // Don't allow any of these error = (illegalChars.test(strng)); return error; } // The following lines test the function with a string containing illegal chars. var teststring = "Here is a str&ing with illegal characters"; var errmsg = checkString(teststring); document.writeln("Testing \"<b>" + teststring + "</b>\" results in <b>" + errmsg + "</b><p>"); var teststring = "Here is a string without any illegal characters"; var errmsg = checkString(teststring); document.writeln("Testing \"<b>" + teststring + "</b>\" results in <b>" + errmsg + "</b><p>"); </script> </head> <body> </body> </html>
0
#6 28 Days Ago
•
•
•
•
I simplified my test script. It still seems to find illegal characters wherever they are in the test string.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Test regex punctuation filter</title> <script type="text/javascript"> function checkString (strng) { var error = false; var illegalChars = /[\u0021-\u002f\u003a-\u0040\u005b-\u005e\u0060\u007b-\u007e]/g; // Don't allow any of these error = (illegalChars.test(strng)); return error; } // The following lines test the function with a string containing illegal chars. var teststring = "Here is a str&ing with illegal characters"; var errmsg = checkString(teststring); document.writeln("Testing \"<b>" + teststring + "</b>\" results in <b>" + errmsg + "</b><p>"); var teststring = "Here is a string without any illegal characters"; var errmsg = checkString(teststring); document.writeln("Testing \"<b>" + teststring + "</b>\" results in <b>" + errmsg + "</b><p>"); </script> </head> <body> </body> </html>
This user has a spatula. We don't know why, but we are afraid.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
In that regex for illegal characters I should not have put the \g modifier at the end. As soon as we find the first illegal character that is all we need to know. The /g global modifier attempts to match all illegal characters in the string starting at the position where it previously found a match. All we need to know is whether there is at least one illegal character in the string so we don't need /g and it is somehow giving us inconsistent results.
In your script, try replacing the regex like this:
In your script, try replacing the regex like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Test regex punctuation filter</title> <script type="text/javascript"> function checkString (strng) { var error = false; //var illegalChars = /[\u0021-\u002f\u003a-\u0040\u005b-\u005e\u0060\u007b-\u007e]/g; // The /g (for global) is a goof. var illegalChars = /[\u0021-\u002f\u003a-\u0040\u005b-\u005e\u0060\u007b-\u007e]/; // NOT global. error = (illegalChars.test(strng)); return error; } // The following lines test the function with a string containing illegal chars. var teststring = "Java$cript"; //Illegal character. Test should return 'true' var errmsg = checkString(teststring); document.writeln("Testing \"<b>" + teststring + "</b>\" results in <b>" + errmsg + "</b><p>"); var teststring = "Javascript"; //No illegal character. Test should return 'false' var errmsg = checkString(teststring); document.writeln("Testing \"<b>" + teststring + "</b>\" results in <b>" + errmsg + "</b><p>"); </script> </head> <body> </body> </html>
![]() |
Similar Threads
- check string for illegal characters (C#)
- rename text file by current date (VB.NET)
- Sending Form (PHP)
- Remove Non Printing Characters From Text (PHP)
- Checking for illegal non-numeric characters (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: HELP! to autoplay or setinterval to YUI carousel
- Next Thread: how to show a larger input field with mouseover?
| Thread Tools | Search this Thread |
.net actionlistener adobe adobeacrobat adobereader ajax ajaxcode ajaxexample arithmetic array asp.net automatically c# cakephp cart checkbox class close codes createrange() data debugger decimal dependent development dictionary dom download editor email epilepsy events excel explorer ext external file firefox flash focus form frameworks getselection google griefers gxt hackers hiddenvalue highlightedword html htmlform iframe import java javascript jquery jsf jsfile jsp logfiles loop mp4 mysql news output path paypal pdf php problem programming python regex registrationform runtime safari scriptlets scroll security session shopping singleton size software string synchronous text unicode validation variables vbnet vulnerability webservice white window windowsxp wysiwyg xlwt xml zeroday






