943,724 Members | Top Members by Rank

Ad:
Apr 24th, 2009
0

Javascript Validation

Expand Post »
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

JavaScript Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
knrakesh is offline Offline
35 posts
since Jul 2008
Apr 25th, 2009
0

Re: Javascript Validation

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...

javascript Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Ajax: auto refreshing a xml file load
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Two actions with one submit button ???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC