How to Validate Special Characters Using JavaScript in Forms?

14 Jan , 2013  JavaScript 

This tutorial is to explain and find a method on how to validate special characters using JavaScript. A simple form validation for special characters with the help of JavaScripts.

JavaScript-Special-Character-Form-validation

Form validation is an important to any website programming. If you own a website, it is then common that you have a contact form or any other form for registration, login and so on. It is necessary to do a server side and client side validation of the form before someone hits the submit button. Otherwise, the form reaching you may have junk data and inappropriate contents.

Validating a HTML form for special characters is something which has been a concern for programmers and web designers. Today, we will see how a form can be validated for special characters with demo. Below you can find the demo and download of the demonstration.

DEMO | DOWNLOAD

The validation formula we use here is simple. We will describe the code as we go on.

HTML Markup:

The form above has something called as a onsubmit attribute in the form tag. The onsubmit attribute tells the browsers that when a user submits the form or in other words clicks the submit button of the form, then activate the JavaScript code succeeding. We call the validate() function when the submit button in the form is hit.

JavaScript:

In the above JavaScript, we basically create a variable to take all the special characters, which is then for looped for the entry in the form. If the function finds a special character matched with the variable, then the alert pops up then and there. We create two fields of input with two different names as field1 and field2, and hence two for loops.

DEMO | DOWNLOAD

There are different methods to validate the form for special characters. This is one of the easy way to do so. If you have some other code which does the same thing, that would definitely help our readers.



, , , ,

Comments are closed.