QAds
12 Ay Taksit
Samsung SL30 10MP Digital Camera with 3x Optical Zoom and 2.5 inch LCD
$48.10
Coby 2.8-Inch LCD Touchscreen Video MP3 Player
$36.20
Apple iPod touch 32 GB
$138.10
Apple iPad 64gb Wi-Fi
$447.59
:: HOME » JavaScript » Validating Form Data
 
Validating Form Data

Most of WEB developer has a form in their WEBsites. When you submit a form it sends some data to server and return result to you. Sometimes we need to get valid data for example email address. For that we can write a control function with dynamic language like PHP or asp. But at this time to validate form data we always need to send data to server and get results. It increase the server-client traffic. To reduce this traffic we can use JavaScript to validate for during submission.  But don’t forget that this does not provide security and any user can by-pass your JavaScript controllers and send any data they want to your server. Thus you need a second  input controller  in server side if it is required.

 

Lets see how to develop form validation script.

 

At first wee need to run JavaScript function at submitting form thus we need to add such code inside form tag.

 

<form action="index.PHP" method="post" onSubmit = "return ourFormValidator(this)">

 

As you see we need to use our JavaScript function with this which defines our form and we need to return function result because at this time, if our function return false, form wont be submitted else it will.

 

Then for example this is login form and we request users email address and password. Email should be valid email address and password should be not empty.

 

<input type="text" name="email">

<input type="password" name="password">

 

After that lets write our JavaScript ourFormValidator function.

 

 

<script type="text/JavaScript">

var EMAIL = /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;

Function ourFormValidator( form)

{

If(form.getElementByName('email').value == "") {

Alert('Please enter email address');

Return false;

}

 

If(form.getElementByName('password').value == "") {

Alert('Please neter your password');

Return false;

}

 

If(!EMAIL.test(form.getElementByName('email').value){

Alert('Please enter valid email address!');

Return false;

}

 

Return true;

 

}

</script>

 

This is the basic form control function. At first we define valid email format. Then in our function we check whether email and password values are empty or not. Then we test email value with our valid email format and if it return false we shows alert to enter valid email address. Otherwise we return true to submit form data to server.

 

I want to give you some input format to use and you can find more at internet :

 

 

var NUMBER = /^[\-]?\d*$/ ;

var REAL = /^[\-]?\d*\.?\d*$/;

 

var TEXTONLY = /^[a-zA-Z\.\-\s]*$/ ;

var PASSWORD = /^\w+$/ ;

var TEXT = /^[^<^>]*$/;

 

var DATE = /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/;

var TIME = /^(\d{1,2})\:(\d{1,2})\:(\d{1,2})$/;

 

var EMAIL = /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;

var PHONE = /^[\d\s\-]+$/;

 


JavaScript Validating Form Data JavaScript Validating Form Data JavaScript Validating Form Data
JavaScript Validating Form Data
NEW QUESTION
 
Your E-Mail Address
»
Your e-mail will never be shown!  
   
 
Question
»
Please type your question and please use [CODE] tag for your codes!  
  Add CODE Quote BOLD ITALIC Underline Align Left Align Center Align Right Add Line  
   
 
Human Check
»
Enter human check code below!  
   
 
       
 
* Your Question will be answered as soon as possible, probably within 24 hours!
JavaScript Validating Form Data JavaScript Validating Form Data JavaScript Validating Form Data
JavaScript Validating Form Data
 
Website Designer, Search Engine Optimization Expert, Ajax Developer,Developed And Designed By Mumin Celik With Using PHP, MySQL, CSS, JavaScript, Ajax And Search Engine Optimizer Sinema ve Film Rehberi Sudoku Oyunu