Niixie Posted November 13, 2011 Share Posted November 13, 2011 Hey people on phpfreaks! On my website, I'm trying to get to know javascript. So, i made this code where its supposed to do a small validation on the entered data, and when I press the submit button, a alert box appears, BUT, when i close that alert box, the fields resets?... I tryed to prevent this with event.preventDefault both before and after the alert box? CODE: var name = document.getElementById('name-field'); var email = document.getElementById('email-field'); var send = document.getElementById('send-field'); if(!name.value==""){ if(!email.value==""){ if(emailValidator(email, "Indtast en gyldig emailaddresse!")){ alert("Tak for din email "+name.value+"!\n\nDin email vil blive læst så hurtigt som muligt af vores staff."); } }else{ alert("Email feltet må ikke være tomt!"); email.focus(); } }else{ alert("Navn feltet må ikke være tomt!"); name.focus(); } Quote Link to comment https://forums.phpfreaks.com/topic/251073-preventing-page-refresh-after-submit/ Share on other sites More sharing options...
Network_ninja Posted November 14, 2011 Share Posted November 14, 2011 add this code after your alert. return false; Quote Link to comment https://forums.phpfreaks.com/topic/251073-preventing-page-refresh-after-submit/#findComment-1287952 Share on other sites More sharing options...
Niixie Posted November 15, 2011 Author Share Posted November 15, 2011 Thank you, but it didn't work? Current code: if(!name.value==""){ if(!email.value==""){ if(emailValidator(email, "Indtast en gyldig emailaddresse!")){ alert("Tak for din email "+name.value+"!\n\nDin email vil blive læst så hurtigt som muligt af vores staff."); } }else{ alert("Email feltet må ikke være tomt!"); email.focus(); return false; } }else{ alert("Navn feltet må ikke være tomt!"); name.focus(); return false; } Quote Link to comment https://forums.phpfreaks.com/topic/251073-preventing-page-refresh-after-submit/#findComment-1288331 Share on other sites More sharing options...
Network_ninja Posted November 16, 2011 Share Posted November 16, 2011 make sure also that you have put return upon calling the function: <input type='submit' name='submit' value='Submit' onclick='return jsfunction()' /> Quote Link to comment https://forums.phpfreaks.com/topic/251073-preventing-page-refresh-after-submit/#findComment-1288568 Share on other sites More sharing options...
Niixie Posted November 16, 2011 Author Share Posted November 16, 2011 Thank you, that worked! Quote Link to comment https://forums.phpfreaks.com/topic/251073-preventing-page-refresh-after-submit/#findComment-1288623 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.