Validations in Javascript


Form validation normally used to occur at the server, after the client had entered all the necessary data and then pressed the Submit button. If the data entered by a client was incorrect or was simply missing, the server would have to send all the data back to the client and request that the form be resubmitted with correct information. This was really a lengthy process which used to put a lot of burden on the server. JavaScript provides a way to validate form's data on the client's computer before sending it to the web server. Form validation generally performs two functions. <form action="#" name="myForm" onsubmit="return(validate());"> <table cellspacing="2" cellpadding="2" border="1"> <tr> <td align="right">Name</td> <td><input type="text" name="Name" /></td> </tr> <tr> <td align="right">EMail</td> <td><input type="text" name="EMail" /></td> </tr> <tr> <td align="right">Zip Code</td> <td><input type="text" name="Zip" /></td> </tr> <tr> <td align="right">Country</td> <td> <select name="Country"> <option value="-1" selected>[choose yours]</option> <option value="1">USA</option> <option value="2">UK</option> <option value="3">INDIA</option> </select> </td> </tr> <tr> <td align="right"></td> <td><input type="submit" value="Submit" /></td> </tr> </table> </form>

Simple Form Validation


<script type="text/javascript"> function validate() { if( document.myForm.Name.value == "" ) { alert( "Please provide your name!" ); document.myForm.Name.focus() ; return false; } if( document.myForm.EMail.value == "" ) { alert( "Please provide your Email!" ); document.myForm.EMail.focus() ; return false; } if( document.myForm.Zip.value == "" || isNaN( document.myForm.Zip.value ) || document.myForm.Zip.value.length != 5 ) { alert( "Please provide a zip in the format #####." ); document.myForm.Zip.focus() ; return false; } if( document.myForm.Country.value == "-1" ) { alert( "Please provide your country!" ); return false; } return( true ); } </script>

Animation in Javascript


JavaScript provides the following two functions to be frequently used in animation programs.
  • setTimeout( function, duration − This function calls function after duration milliseconds from now.
  • setInterval(function, duration) − This function calls function after every duration milliseconds.
  • clearTimeout(setTimeout_variable) − This function calls clears any timer set by the setTimeout() functions.

Manual Animations


Example: <script type="text/javascript"> <!-- var imgObj = null; function init(){ imgObj = document.getElementById('myImage'); imgObj.style.position= 'relative'; imgObj.style.left = '0px'; } function moveRight(){ imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px'; } window.onload =init; //--> </script> </head> <body> <form> <img id="myImage" src="/images/html.gif" /> <p>Click button below to move the image to right</p> <input type="button" value="Click Me" onclick="moveRight();" /> </form>
->  you can check this example in browser.

Javscript Debugging


Debugging is not easy. But fortunately, all modern browsers have a built-in JavaScript debugger. Built-in debuggers can be turned on and off, forcing errors to be reported to the user. With a debugger, you can also set breakpoints (places where code execution can be stopped), and examine variables while the code is executing. Normally, otherwise follow the steps at the bottom of this page, you activate debugging in your browser with the F12 key, and select "Console" in the debugger menu.
Chrome


Open the browser.

From the menu, select tools.

From tools, choose developer tools.

Finally, select Console.
            
Firefox Firebug 
              
Open the browser.
Go to the web page:
http://www.getfirebug.com
Follow the instructions how to:
install Firebug



Internet Explorer  

Open the browser.
From the menu, select tools.
From tools, choose developer tools.
Finally, select Console.


 Opera 

Open the browser.
Go to the webpage:
http://dev.opera.com
Follow the instructions how to:
add a Developer Console button to your toolbar.
            
CONTENTS