views:

7227

answers:

4

Special characters <,>,%,'',"",$,^ 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 submit but the function is not recognised on click.

Please help me in this java script to acheive this functionality.

A: 

Try something like

<form ... onsubmit="function()">

In function you can get text from your textarea or what you are using. If data is valid function () should return true. Otherwise form wouldn't be submitted.

Roman
A: 
function isSpclChar(){
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
  for (var i = 0; i < document.qfrm.q.value.length; i++) {
    if (iChars.indexOf(document.qfrm.q.value.charAt(i)) != -1) {
    alert ("The box has special characters. \nThese are not allowed.\n");
    return false;
  }
    }
}
Teckie
A: 

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;
   }
}

Hitesh Agarwal http://www.hiteshagrawal.com