Javascript Validation

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2008
Posts: 32
Reputation: knrakesh is an unknown quantity at this point 
Solved Threads: 0
knrakesh's Avatar
knrakesh knrakesh is offline Offline
Light Poster

Javascript Validation

 
0
  #1
Apr 24th, 2009
Hello Good afternoon Guys
Iam KNR iam having a problem with javascript validation
i want to check validation of all controls at a time means
if i have a form with all fields blank then it has to show message
and if any of the field is filled then it dont have to show the message and
the message about blank field has to shown

  1. if(trim(cname.value) == '')
  2. {
  3. alert('Enter a Name');
  4. cname.focus();
  5. return false;
  6. }
please help me
Last edited by peter_budo; Apr 25th, 2009 at 5:07 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Javascript Validation

 
0
  #2
Apr 25th, 2009
This demo will alert the user, depending on which is field is empty. Lets say for example, ( if field 1 is empty then, field 1 will be identified as invalid entry and as well as with the other field's also).

Don't worry--If you need a simple demo, im sure some other posters will provide another format of this code.
Hope it helps and good day to you...

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <meta http-equiv="Content-Style-Type" content="text/css" />
  9. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  10. <title>JavaScript Demo</title>
  11. <style id="internal" type="text/css">
  12. /* <![CDATA[ */
  13. label { display : block; margin : 0em 0em 1em 0em; }
  14. /* ]]> */
  15. </style>
  16. <script type="text/javascript">
  17. // <![CDATA[
  18. var validate, showError;
  19. var i, x, invalidEntry, msg, error = [];
  20.  
  21. showError = function( error ) {
  22. msg = "There were some problems...\n";
  23. for ( i = 0; i < error.length; i++ ) {
  24. invalidEntry = i + 1;
  25. msg += "\n" + invalidEntry + ". " + error[i];
  26. } alert( msg );
  27. window.location.reload(); return;
  28. };
  29.  
  30. validate = function( form ) {
  31. form = ( document.getElementById ) ? document.getElementById("testform") : document.all.testform;
  32. if ( form ) {
  33. for ( var x = 1; x < form.elements.length; x++ ) {
  34. if ( eval("form.txt" + x + ".value.length") <= 0 ) {
  35. error[error.length] = "Field " + x + " requires a valid entry!";
  36. }
  37. }
  38. if ( error.length > 0 ) {
  39. showError( error );
  40. return false;
  41. }
  42. } alert("Thank you for visiting my site!"); return false;
  43. };
  44.  
  45. // ]]>
  46. </script>
  47. </head>
  48. <body>
  49. <div id="content">
  50. <form id="testform" action="#" onsubmit="return validate( this );">
  51. <div>
  52. <label for="txt1">Field 1: <input type="text" id="txt1" name="txt1" value="" size="20" /></label>
  53. <label for="txt2">Field 2: <input type="text" id="txt2" name="txt2" value="" size="20" /></label>
  54. <label for="txt3">Field 3: <input type="text" id="txt3" name="txt3" value="" size="20" /></label>
  55. <input type="submit" id="sbm" name="sbm" value="submit" />
  56. </div>
  57. </form>
  58. </div>
  59. </body>
  60. </html>
Last edited by essential; Apr 25th, 2009 at 4:22 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC