abdelelbouhy Posted October 27, 2010 Share Posted October 27, 2010 hello guysif you please tell me what is wrong in this script i'm validating form and displaying error message and give the element focus what happing is the message display and the element get focused for milliseconed then disappear and the element lose focus i can't see anything wrong function validateForm(form){ var form = document.getElementById(form); var elements = form.elements; var errorSpan = document.createElement('span'); errorSpan.appendChild(document.createTextNode('required')); errorSpan.className = 'error'; var pattern = /(^| )checkRequired( |$)/; var emailPattern = /(^| )checkEmail( |$)/; var email = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/; for(var i = 0 ; i < elements.length; i++){ if(pattern.test(elements.className) && elements.value == ''){ elements.focus(); elements.parentNode.insertBefore(errorSpan,elements.nextSibling); return false; } if(emailPattern.test(elements.className) && !email.test(elements.value)){ elements.focus(); elements.parentNode.insertBefore(errorSpan,elements.nextSibling); return false; } } return true; } Link to comment Share on other sites More sharing options...
ShadowMage Posted October 27, 2010 Share Posted October 27, 2010 This is just a wild guess, but try reversing the order of your focus and insert:elements.focus();elements.parentNode.insertBefore(errorSpan,elements.nextSibling);toelements.parentNode.insertBefore(errorSpan,elements.nextSibling);elements.focus();I'm wondering if the errorSpan is receiving the focus when it's inserted. Link to comment Share on other sites More sharing options...
abdelelbouhy Posted October 27, 2010 Author Share Posted October 27, 2010 This is just a wild guess, but try reversing the order of your focus and insert:elements.focus();elements.parentNode.insertBefore(errorSpan,elements.nextSibling);toelements.parentNode.insertBefore(errorSpan,elements.nextSibling);elements.focus();I'm wondering if the errorSpan is receiving the focus when it's inserted. no it doesn't get focus i jus change its color with css i found what was wrong and here is the final scriptfunction validateForm(form){ var form = document.getElementById(form); var elements = form.elements; var errorSpan = document.createElement('span'); errorSpan.appendChild(document.createTextNode('*required')); errorSpan.className = 'error'; var email = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/; for( var i = 0 ; i < elements.length; i++){ if(/(^| )checkRequired( |$)/.test(elements.className)){ if(elements.value == ''){ elements.focus(); if(elements.nextSibling.nodeName.toLowerCase() != 'span'){ elements.parentNode.insertBefore(errorSpan,elements.nextSibling); } return false; }else{ if(elements.nextSibling.nodeName.toLowerCase() == 'span'){ elements.parentNode.removeChild(elements.nextSibling); } } } if(/(^| )checkEmail( |$)/.test(elements.className)){ if(!email.test(elements.value)){ elements.focus(); if(elements.nextSibling.nodeName.toLowerCase() != 'span'){ elements.parentNode.insertBefore(errorSpan,elements.nextSibling); } return false; }else{ if(elements.nextSibling.nodeName.toLowerCase() == 'span'){ elements.parentNode.removeChild(elements.nextSibling); } } } } return true; } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.