eits Posted March 9, 2009 Share Posted March 9, 2009 I have the following code which checks a disclaimer box is ticked and also checks a field (productuse) has some text in. I also have a field called custemail which I want to validate is an email address otherwise return false. Could somebody please help me add the appropriate 'else if' statement to do this, I've tried several scripts out there but just can't get them to work with this current script: <script LANGUAGE="JavaScript"> <!-- function validate(frm) { // // Check the Email field to see if any characters were entered // if (frm.productuse.value.length == 0) { alert("Please tell us why you require this product.") frm.productuse.focus() return false } else if (frm.disclaimer.checked == false) { alert("You must read the disclaimer before requesting a quote.") frm.disclaimer.focus() return false } } //--> </SCRIPT> Sorry, I'm a complete javascript n00b! Quote Link to comment https://forums.phpfreaks.com/topic/148589-please-help-with-this/ Share on other sites More sharing options...
Adam Posted March 9, 2009 Share Posted March 9, 2009 There's no existing function in JavaScript to validate an email (at least to my knowledge) so you'd probably need to use a custom function (there's loads on Google - I'll just use the one from first result) .. <script LANGUAGE="JavaScript"> <!-- function validate(frm) { // // Check the Email field to see if any characters were entered // if (frm.productuse.value.length == 0) { alert("Please tell us why you require this product."); frm.productuse.focus(); return false; } else if (frm.disclaimer.checked == false) { alert("You must read the disclaimer before requesting a quote."); frm.disclaimer.focus(); return false; } else if (is_email(frm.custemail.value) == false) { alert("Please enter a valid email address."); frm.custemail.focus(); return false; } } function is_email(str) { var at="@"; var dot="."; var lat=str.indexOf(at); var lstr=str.length; var ldot=str.indexOf(dot); if (str.indexOf(at)==-1){ return false; } if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false; } if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false; } if (str.indexOf(at,(lat+1))!=-1){ return false; } if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false; } if (str.indexOf(dot,(lat+2))==-1){ return false; } if (str.indexOf(" ")!=-1){ return false; } return true; } //--> </SCRIPT> Not tested but should work no probs! Adam Quote Link to comment https://forums.phpfreaks.com/topic/148589-please-help-with-this/#findComment-780330 Share on other sites More sharing options...
eits Posted March 9, 2009 Author Share Posted March 9, 2009 Thankyou very much I really appreciate it - will try it in a little while. I should really take some time to learn Javascript, think 'Procrastination' is the keyword there though! Quote Link to comment https://forums.phpfreaks.com/topic/148589-please-help-with-this/#findComment-780339 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.