JavaScript help (part of form validation)?
-
Hi and thank you in advance for helping me and giving me your tips. I am learning javascript and i just got to the form validation point in the book i have. Anyways here is the problem i have and here is the code: When the user doesn't put anything in the Username field and just leaves it blank on the form submit javascript should check that username field and make sure it is NOT blank, if it is blank it is to write a message next to the Username field. It does everything perfectly except for the last step, it writes the certain error description next to the Username field but it disappears after a second. I think it has to do witht he page RELOADING and clearing all fields in the form. Code ========== <head> <!-- Validate login script --> <script language="javascript" type="text/javascript"> <!-- var whiteSpace = " \t\n\r"; var errorTipInsertSpot; var errorTip; function isEmpty(s) { var i; if((s == null) || (s.length == 0)) return true; // Search string looking for characters that are not whitespace for (i = 0; i < s.length; i++) { var c = s.charAt(i); if (whiteSpace.indexOf(c) == -1) return false; } // All characters are whitespace. return true; } function validate() { if (isEmpty(document.signIn.username.value)… { errorTipInsertSpot = document.getElementById('usernameError')… errorTip = document.createTextNode("Username can't be empty"); errorTipInsertSpot.setAttribute('cla… 'formError') errorTipInsertSpot.appendChild(error… } } //--> </script> </head> <body> ..... <table border="0" cellspacing="0" cellpadding="0"> <form action="" method="post" name="signIn" id="signIn" onsubmit="return validate()"> <tr> <td width="26"></td> <td width="136">Username:</td> <td width="130"><input name="username" type="text" id="username" size="15" maxlength="25" /></td> <td width="130"><div id="usernameError"> </div></td> </tr> .... ========== I left most of the XHTML and code out because it is not needed. Anyways i havent finished the part of the code that tells the user if the form field has information that will pass. So my form javascript isn't complete BUT that part of the code i have should function perfectly, the only stuff i left out was email & password check. Thanks again for helping me, Jason
-
Answer:
In your function validate() INSIDE your if statement return false if(isEmpty(whatever)) { //do some stuff return false; //this won't let the form to trigger the action }
Jason at Yahoo! Answers Visit the source
Related Q & A:
- How to style the HTML5 form validation messages?Best solution by Stack Overflow
- How does the form validation work on angularjs with ionic?Best solution by blog.nraboy.com
- I need help with getting my first part time job.Best solution by Yahoo! Answers
- Looking for part time job help?Best solution by Yahoo! Answers
- Need help debugging a javascript?Best solution by allwebdevhelp.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.