Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special character validation using JavaScript

Special characters <, >, %, '', "", $ and ^ are not allowed in a textbox. I need to put a validation check to restrict these characters on submit along with the null check.

I wrote entire validation code in a function and calling it on click of the submit button, but the function is not recognised on click.

Please help me write some JavaScript code to achieve this functionality.


1 Answers

A much simpler way is to use indexOf in javascript,

function isSpclChar(){
   var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
   if(document.qfrm.q.value.indexOf(iChars) != -1) {
     alert ("The box has special characters. \nThese are not allowed.\n");
     return false;
   }
}
like image 78
Hitesh Agarwal Avatar answered Apr 24 '26 07:04

Hitesh Agarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!