By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

special character check in text box

P: 2
Hi guys,

I want to check whether the user has entered any special characters in a text box.

If the user enters, i want to display an alert message stating that he cant enter a special character (like ).

I have written a code wherein i am specifying the list of characters that are not allowed.

It would be better if i can give a list of characters that are allowed instead of list of characters that are not allowed. And check whether any of the character in the text box, is not in the list.

Can u guys suggest me how to go about doing it.

here is my piece of code:

var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < document.basic_submit_form.comp_name.value.length; i++) {
if (iChars.indexOf(document.basic_submit_form.comp_na me.value.charAt(i)) != -1) {
errorMsg += "* Company name entered has special characters. Please remove them and try again.\n";
noOfErr ++;
}
}
Aug 7 '07 #1
Share this Question
Share on Google+
3 Replies


dmjpro
100+
P: 2,476
Hi guys,

I want to check whether the user has entered any special characters in a text box.

If the user enters, i want to display an alert message stating that he cant enter a special character (like ).

I have written a code wherein i am specifying the list of characters that are not allowed.

It would be better if i can give a list of characters that are allowed instead of list of characters that are not allowed. And check whether any of the character in the text box, is not in the list.

Can u guys suggest me how to go about doing it.

here is my piece of code:

var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < document.basic_submit_form.comp_name.value.length; i++) {
if (iChars.indexOf(document.basic_submit_form.comp_na me.value.charAt(i)) != -1) {
errorMsg += "* Company name entered has special characters. Please remove them and try again.\n";
noOfErr ++;
}
}

Welcome to TSDN!!!

Do u know regular expression?
Then it wud be better to handle.
You can check the key value range.

Expand|Select|Wrap|Line Numbers
  1. //in IE ...
  2. event.keyCode
  3. //In others
  4. event.which
  5.  
Aug 7 '07 #2

P: 2
But I want to allow the user to enter the special characters in the text field.
The alert message will be shown only when the form is submitted.

As i understand, keyCode checks the key that is typed.

Can u suggest with unicode?


Thanks
Aug 7 '07 #3

acoder
Expert Mod 15k+
P: 16,027
But I want to allow the user to enter the special characters in the text field.
The alert message will be shown only when the form is submitted.

As i understand, keyCode checks the key that is typed.

Can u suggest with unicode?
Use regular expressions, but check when you submit the form.

See tutorial link and specifically for Javascript.

Use the form onsubmit event handler to call a validation function.
Aug 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.