简体   繁体   中英

Confirm msg before submitting a form

i am validating a form and then asking for the confiormation through javascript… so on submit i have called two function name validate() & make_confirm()..

onsubmit="return(validate() && show_alert(this));"

by this i am partially able to call both function but confirmation part is not working fine i have to redirect it to another page while pressing OK but its not going please help me out

validate & make_sure() function is as like:

function validate() {
           if(document.getElementById('cname').value == '')
            {
                alert('Please enter your name');
                document.getElementById('cname').focus();
                return false;
            }
            else if(document.getElementById('address').value == '')
            {
                alert('Please enter your address');
                document.getElementById('address').focus();
                return false;
            }
            else if(document.getElementById('city').value == '')
            {
                alert('Please choose your city');
                document.getElementById('city').focus();
                return false;
            }
            else if(document.getElementById('state').value == '')
            {
                alert('Please enter your state');
                document.getElementById('state').focus();
                return false;
            }
function make_sure() {
          if(confirm("Do you really want to do this?"))
            document.forms[0].submit();
          else
            return false;
        }

Use one function for validate and confirm and set action of form to redirect form current page to another page.

function validateAndConfirm() {
    if( isEmpty(id('cname').value) ) {
        alert('Please enter your name');
        id('cname').focus();
        return false;
    }
    else if( isEmpty(id('address').value) ) {
        alert('Please enter your address');
        id('address').focus();
        return false;
    }
    else if( isEmpty(id('city').value) ) {
        alert('Please choose your city');
        document.getElementById('city').focus();
        return false;
    }
    else if( isEmpty(id('state').value) ) {
        alert('Please enter your state');
        id('state').focus();
        return false;
    } else {
        if(confirm("Do you really want to do this?")) {
            document.forms[0].submit();
        }
        else {
            return false;
        }
    }
}

function isEmpty(val){
    return (typeof val == 'undefined' || val === undefined || val == null || val.length <= 0) ? true : false;
}

// this function help to simplify you writing : document.getElementById to just id
function id(sid){
   return document.getElementById(sid);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM