Tuesday, 15 September 2015

JavaScript how to use for each -



JavaScript how to use for each -

<!doctype html> <html> <body> <table width="100%" border="0" cellspacing="0" cellpadding="0" id="errortable" class="alert alert-danger alert-warning"> <!-- validating city start --> <div id="divname"> <tr> <td width="3"></td> <td width="23"><span class="table_row_error_text"><img id="imgsum" src="/images/error.gif" class="image" style="display: none;" ></span> </td> <td width="5"></td> <td><span class="table_row_error_text"> <span id="errorsum" /></span> </td> </tr> </div> <!-- validating city ends --> <!-- validating zip code start --> <div id="divtype"> <tr> <td width="3"></td> <td width="23"><span class="table_row_error_text"><img id="imgdesc" src="/images/error.gif" class="image" style="display: none;" ></span> </td> <td width="5"></td> <td><span class="table_row_error_text"> <span id="errordesc" /></span> </td> </tr> </div> <!-- validating zip code ends --> </table> summary: <input id="summaryofalert" type="text" /> <br> <br> description: <input id="textarea3" type="text" /> <br> <br> save: <input type="submit" onclick="return validatetext()" /> </body> <script> function validatetext() { alert("hi"); var summary = document.getelementbyid("summaryofalert").value; var description = document.getelementbyid("textarea3").value; var letters = /^[a-za-z0-9._\-,\s]+$/; var j; j = 0; if (summary != "") { if (!summary.match(letters)) { document.getelementbyid('errorsum').innerhtml = "in summary please come in a-za-z0-9-,_."; document.getelementbyid("divname").style.display = 'block'; document.getelementbyid("imgsum").style.display = 'block'; j++; } } if (description != "") { if (!description.match(letters)) { document.getelementbyid('errordesc').innerhtml = "in description please come in a-za-z0-9-,_."; document.getelementbyid("divtype").style.display = 'block'; document.getelementbyid("imgdesc").style.display = 'block'; j++; } } if (j == 0) { homecoming true; } else homecoming false; } </script> </html>

above html/javascript code have developed front end end validation special characters. working fine problem whenever entered special chars <>%&(^ in both text fields , after submitting showing error message shown in summary please come in a-za-z0-9-,. in description please come in a-za-z0-9-,.

but when removed special chars in 1 of text fields , submitting values 1 time again both error messages displaying instead of 1 error message

you showing messages not hiding them anywhere. seek this:

if(!summary.match(letters)) { document.getelementbyid('errorsum').innerhtml="in summary please come in a-za-z0-9-,_."; document.getelementbyid("divname").style.display='block'; document.getelementbyid("imgsum").style.display='block'; j++; } else { document.getelementbyid('errorsum').innerhtml=""; document.getelementbyid("divname").style.display='none'; document.getelementbyid("imgsum").style.display='none'; }

and same things description. create function don't have write twice.

javascript

No comments:

Post a Comment