toolman Posted September 11, 2012 Share Posted September 11, 2012 Hi, I have this code, but I want it to validate when the form is submitted rather than when the user starts to enter text. How would I do that? function checkkey(v) { if (/\W/.test(v.value)) { alert("Please enter alphanumerics only"); return false; } return true; } <form name="snazzyForm" action="index.html" method="POST" onsubmit="return validateForm()" > <input type="text" name="textfield" onKeyUp="return checkkey(this)" /> </form> Link to comment https://forums.phpfreaks.com/topic/268269-onsubmit-rather-than-onkeyup/ Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 Combine the two functions into one, and set it as the onsubmit function. Link to comment https://forums.phpfreaks.com/topic/268269-onsubmit-rather-than-onkeyup/#findComment-1377129 Share on other sites More sharing options...
toolman Posted September 11, 2012 Author Share Posted September 11, 2012 Thanks. I now have this: function validateForm() { var x=document.forms["snazzyForm"]["textfield"].value; if (x==null || x=="") { alert("First name must be filled out"); return false; } if (/\W/.test(v.value)) { alert("Please enter alphanumerics only"); return false; } return true; } } and <form name="snazzyForm" action="index.html" method="POST" onsubmit="return validateForm()" > <input type="text" name="textfield" /> </form> but it doesn't work Link to comment https://forums.phpfreaks.com/topic/268269-onsubmit-rather-than-onkeyup/#findComment-1377133 Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 You wrote them. Look at the code, what is it doing? Link to comment https://forums.phpfreaks.com/topic/268269-onsubmit-rather-than-onkeyup/#findComment-1377134 Share on other sites More sharing options...
toolman Posted September 11, 2012 Author Share Posted September 11, 2012 I can't work it out. I now have this on my onsubmit: onsubmit="return (validateForm() && checkkey(this))" but I can't work out how to combine the functions. Link to comment https://forums.phpfreaks.com/topic/268269-onsubmit-rather-than-onkeyup/#findComment-1377136 Share on other sites More sharing options...
Christian F. Posted September 12, 2012 Share Posted September 12, 2012 How does the script know what values to check? Answer this one, and you have solved your problem. Link to comment https://forums.phpfreaks.com/topic/268269-onsubmit-rather-than-onkeyup/#findComment-1377170 Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 You should be using a JS debugger, it would point out the problem. Link to comment https://forums.phpfreaks.com/topic/268269-onsubmit-rather-than-onkeyup/#findComment-1377209 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.