简体   繁体   English

直到所有必填字段都填满后,如何使JavaScript警报不运行

[英]how to make javascript alert not run until all required fields are filled

I have created a Signup form. 我已经创建了一个注册表单。 I need some help as i am new to web development. 我是Web开发的新手,我需要一些帮助。 The Problem is that When I press submit button. 问题是当我按下提交按钮时。 javascript alert runs. javascript警报运行。 even when all fields are not filled.I want that javascript alert (which is function Data()) not run until all required fields are filled. 即使所有字段都未填写,我也要等到所有必填字段都填满后才能运行JavaScript提醒(即Data()函数)。

JavaScript Code JavaScript代码

<script>
    function Data() {
        alert("User ID: " + textfield1.value + '\n' + "Password: " + textfield2.value + '\n' + "Conform Password: " + textfield3.value + '\n' + "First Name: " + textfield4.value + '\n' + "Last Name: " + textfield5.value + '\n' + "Email: " + textfield6.value + '\n' + "Company: " + textfield7.value + '\n' + "Phone: " + textfield8.value + '\n' + "Fax: " + textfield9.value + '\n' + "Address Line 1: " + textfield10.value + '\n' + "Address Line 2: " + textfield11.value + '\n' + "City: " + textfield12.value + '\n' + "State/Province: " + textfield13.value + '\n' + "Country: " + textfield14.value + '\n' + "Zip/Postal Code: " + textfield15.value + '\n');
        {
            if (input.value != document.getElementById('password').value) {
                alert('Password Must be Matching.');
            } else {
                // input is valid -- reset the error message
                input.setCustomValidity('');
            }
        }
    }
    function inputnumber(evt) {
        var limit = (evt.which) ? evt.which : event.keyCode
        if (limit > 31 && (limit < 48 || limit > 57)) {
            alert("Enter numerals only in this field.");
            return false;
        }

        return true;
    }
    function checkPass() {
        var inputpassword = document.getElementById("textfield2");
        var conformpassword = document.getElementById("textfield3");
        var message = document.getElementById("confirmMessage");
        var goodpassword = "#66cc66";
        var badpassword = "#ff6666";
        if (inputpassword.value == conformpassword.value) {
            conformpassword.style.borderColor = goodpassword;
            message.style.color = goodpassword;
            message.innerHTML = "Password Match!"
        }
        else {
            conformpassword.style.borderColor = badpassword;
            message.style.color = badpassword;
            message.innerHTML = "Password Do Not Match!"
        }
    }


</script>


<body>
<form autocomplete="on">
<h1 style="text-align: center">
    Signup Form</h1>
<table align="center">
    <tr>
        <td>
            User ID
        </td>
        <td>
            <input type="text" id="textfield1" style="border-radius: 7px; border: 2px solid #dadada;"
                autofocus required />
        </td>
    </tr>
    <tr>
        <td>
            Password<sup>*</sup>
        </td>
        <td width="50%">
            <input type="password" maxlength="10" id="textfield2" style="border-radius: 7px;
                border: 2px solid #dadada;" required />
        </td>
    </tr>
    <tr>
        <td>
            Conform Password
        </td>
        <td>
            <input type="password" maxlength="10" id="textfield3" onkeyup="checkPass()" style="border-radius: 7px;
                border: 2px solid #dadada;" required /><span id="confirmMessage"></span>
        </td>
    </tr>
    <tr>
        <td>
            First Name
        </td>
        <td>
            <input type="text" id="textfield4" style="border-radius: 7px; border: 2px solid #dadada;"
                required />
        </td>
    </tr>
    <tr>
        <td>
            Last Name
        </td>
        <td>
            <input type="text" id="textfield5" style="border-radius: 7px; border: 2px solid #dadada;"
                required />
        </td>
    </tr>
    <tr>
        <td>
            Email
        </td>
        <td>
            <input type="email" id="textfield6" style="border-radius: 7px; border: 2px solid #dadada;"
                required />
        </td>
    </tr>
    <tr>
        <td>
            Company
        </td>
        <td>
            <input type="text" id="textfield7" style="border-radius: 7px; border: 2px solid #dadada;" />
        </td>
    </tr>
    <tr>
        <td>
            Phone
        </td>
        <td>
            <input type="text" id="textfield8" onkeypress="return inputnumber(event)" style="border-radius: 7px;
                border: 2px solid #dadada;" required />
        </td>
    </tr>
    <tr>
        <td>
            Fax
        </td>
        <td>
            <input type="text" id="textfield9" onkeypress="return inputnumber(event)" style="border-radius: 7px;
                border: 2px solid #dadada;" />
        </td>
    </tr>
    <tr>
        <td>
            Address Line 1
        </td>
        <td>
            <input type="text" id="textfield10" style="border-radius: 7px; border: 2px solid #dadada;"
                required />
        </td>
    </tr>
    <tr>
        <td>
            Address Line 2
        </td>
        <td>
            <input type="text" id="textfield11" style="border-radius: 7px; border: 2px solid #dadada;" />
        </td>
    </tr>
    <tr>
        <td>
            City
        </td>
        <td>
            <input type="text" id="textfield12" style="border-radius: 7px; border: 2px solid #dadada;"
                required />
        </td>
    </tr>
    <tr>
        <td>
            State/Province
        </td>
        <td>
            <input type="text" id="textfield13" style="border-radius: 7px; border: 2px solid #dadada;"
                required />
        </td>
    </tr>
    <tr>
        <td>
            Country
        </td>
        <td>
            <input type="text" id="textfield14" style="border-radius: 7px; border: 2px solid #dadada;"
                required />
        </td>
    </tr>
    <tr>
        <td>
            Zip/Postal Code
        </td>
        <td>
            <input type="text" id="textfield15" onkeypress="return inputnumber(event)" style="border-radius: 7px;
                border: 2px solid #dadada;" required />
        </td>
    </tr>
    <tr>
        <td>
            <div align="center">
                <input type="submit" value="Submit!" onclick="Data()" style="border-radius: 7px;
                    border: 2px solid #dadada;" /></div>
        </td>
        <td>
            <div align="center">
                <input type="reset" value="Reset" style="border-radius: 7px; border: 2px solid #dadada;" /></div>
        </td>
    </tr>
</table>
</form>

Try that: 试试看:

<form autocomplete="on" onsubmit="Data()">

And remove that from submit button: 并从提交按钮中删除:

<input type="submit" value="Submit!" style="border-radius: 7px;border: 2px solid #dadada;" />

Explanation: 说明:

When you use click on button, it will always fire, because it's click. 当您使用“ click ”按钮时,它将始终触发,因为它是单击的。 If you use submit on form, then it is fired only when form is submitted, which means it's correct and has all required fields filled 如果您在表单上使用submit ,则仅在提交表单时才会触发它,这表示它是正确的并且已填写所有必填字段

UPDATE 更新

In case if you want to run some additional custom validation (like password confirmation), and find form invalid and want to STOP submit process, use this hint: 如果您想运行一些其他的自定义验证(例如密码确认),并且发现表单无效并想要停止提交过程,请使用以下提示:

function Data(e) { //"e" - is an event object, that is being populated on form's submit
    if (input.value != document.getElementById('password').value) {
        alert('Password Must be Matching.');
        e.stopPropagation(); //These two rows KILL the event, so it will not be submited
        e.preventDefault();
    }
}

You should check to make sure each textbox has something in it with a series of if else statements. 您应该检查以确保每个文本框都带有一系列if else语句。

Ex. 例如

if(!document.getElementById("yourtextbox").value.match(/\S/))
{
     window.alert("You must enter a value for yourtextbox");
}
else
{
    if(!document.getElementById("anothertextbox").value.match(/\S/))
    {
       window.alert("You must enter a value for anothertextbox");
    }
    else
    {


         alert("User ID: " + textfield1.value + '\n' + "Password: " + textfield2.value + '\n' + "Conform Password: " + textfield3.value + '\n' + "First Name: " + textfield4.value + '\n' + "Last Name: " + textfield5.value + '\n' + "Email: " + textfield6.value + '\n' + "Company: " + textfield7.value + '\n' + "Phone: " + textfield8.value + '\n' + "Fax: " + textfield9.value + '\n' + "Address Line 1: " + textfield10.value + '\n' + "Address Line 2: " + textfield11.value + '\n' + "City: " + textfield12.value + '\n' + "State/Province: " + textfield13.value + '\n' + "Country: " + textfield14.value + '\n' + "Zip/Postal Code: " + textfield15.value + '\n');
        {
            if (input.value != document.getElementById('password').value) {
                alert('Password Must be Matching.');
            } else {
                // input is valid -- reset the error message
                input.setCustomValidity('');
            }
        }
    }
}

Just validate that your text fields are not empty before calling your alert. 在致电警报之前,只需验证您的文本字段不为空即可。 Change your submit button type from "submit" to "button". 将您的提交按钮类型从“提交”更改为“按钮”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript禁用按钮,直到所有字段都填满 - Javascript disable button until all fields are filled 如何禁用按钮,直到不使用JavaScript(仅HTML5和CSS)填充必填字段 - How to Disable button until required fields are filled without using javascript, just HTML5 and CSS 如何制作需要填充的对象中的所有字段或根本不填充对象中的任何字段? - How to make either all fields in a object required for filling or none of the fields in a object to be filled at all? 如何禁用按钮,直到仅使用 JavaScript 填充所有输入字段? - How to disable a button until all the input fields are filled using only JavaScript? 填写完所有字段后如何提交JavaScript - How to submit for after all the fields are filled in JavaScript 填写所有必填字段时如何防止默认提交 - How to prevent default submission when ALL required fields are filled in 如何禁用按钮直到所有必填字段都已填写 - how to disable a button unti all the required fields are filled 如何检查是否填写了所有必填字段以启用提交按钮? - How to check if all required fields are filled to enable submit button? javascript-将表格上的所有字段设为必填 - javascript - Make all fields on a form required 检查是否在特定的div中填写了所有必填字段 - Check if all required fields are filled in a specific div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM