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> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.