Jump to content

Preventing page refresh after submit


Niixie

Recommended Posts

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();
    }

Link to comment
Share on other sites

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;
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.