I want to verify in JavaScript that my required fields are not blank

前端 未结 3 1897
遥遥无期
遥遥无期 2021-01-25 02:03

Hopefully this is basic JavaScript and the answer is easy since I am new at this. I just want to make sure all required fields are not blank before the user can proceed. In my <

3条回答
  •  天命终不由人
    2021-01-25 02:38

    Try changing the function to this: (NOTE: you have to add more tests to the if clause for the rest of the elements)

    function insert() {
        if (window.XMLHttpRequest) {
            xmlhttp = new XLMHttpRequest();
        } else {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
    
    
        if (document.getElementById('child_name').value === '' ||
            document.getElementById('age').value === '' || 
            ...more tests... || 
            document.getElementById('zip').value === '') {
            alert ("Please enter all required fields.");
            return false;
        } else {
            cn1 = 'child_name'+document.getElementById('child_name').value;
            ag2 = 'age'+document.getElementById('age').value;
            hm3 = 'hometown'+document.getElementById('hometown').value;
            bg4 = 'boy_girl'+document.getElementById('boy_girl').value;
            fn5 = 'first_name'+document.getElementById('first_name').value;
            ln6 = 'last_name'+document.getElementById('last_name').value;
            email = 'email'+document.getElementById('email').value;
            ad8 = 'address1'+document.getElementById('address1').value;
            ad9 = 'address2'+document.getElementById('address2').value;
            ct10 = 'city'+document.getElementById('city').value;
            st11 = 'state'+document.getElementById('state').value;
            zp12 = 'zip'+document.getElementById('zip').value;
    
            xmlhttp.open('POST', 'payment.php', true);
            xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xmlhttp.send(cn1&ag2&hm3&bg4&fn5&ln6&email$ad8&ad9&ct10&st11&zp12); 
        }
    }
    

提交回复
热议问题