简体   繁体   English

JavaScript自定义验证不起作用

[英]JavaScript custom validation not working

Here is my code : 这是我的代码:

<script type="text/javascript">
    function submitform() {
        if(document.getElementById('name').value=='') {
            alert('Please enter a name');
            return false;
        }
    }
</script>
<form action="mail.php" method="post" onsubmit="submitform();">
    <input type="text" id="name" name="name" placeholder="name">
    <input type="submit" value="submit">
</form>

as expected, the form when submitted should call the submitform function, and if the name field is blank, it should return false and give an alert. 如预期的那样,提交表单时应调用submitform函数,如果名称字段为空,则应返回false并发出警报。 But, it just goes through. 但是,它只是通过。 Any explainations? 有什么解释吗?

You need to call the function with return, so that the false value prevents default action (form submission) 您需要使用return调用该函数,以便false值阻止默认操作(表单提交)

<form action="mail.php" method="post" onsubmit="return submitform();">
    <input type="text" id="name" name="name" placeholder="name">
    <input type="submit" value="submit">
</form>

You need to stop a little. 您需要停止一点。

You can use onSubmit, but it's best to delete your input submit and put a button. 您可以使用onSubmit,但是最好删除输入的提交并放一个按钮。 Then on button click you can do what you want and eventually submit the form 然后单击按钮,您可以做您想做的事情并最终提交表单

Form: 形成:

<form action="mail.php" method="post" id="mailForm">
<input type="text" id="name" name="name" placeholder="name">
<button id="submitMailForm">Submit</button>

JS: JS:

$( document ).on( "click", "#submitMailForm", function(e) {
 //My control Here
 //If all ok
 $("#mailForm").submit();
});

You can use jquery instead of javascript for this kind of validation is will be very easy to implement. 您可以使用jquery而不是javascript进行这种验证,这将非常容易实现。

<form action="mail.php" method="post">
  <input type="text" id="name" name="name" placeholder="name">
  <input type="submit" value="submit" id="submit">
</form>

<script>
   $(document).ready(function(){
     $("#submit").click(fucntion(e){
        if($("#name").val() == ""){
       alert("Name is empty");
       e.preventDefault();
     }
     });
   });
</script>

And dont forget to add jquery library before the script tag. 并且不要忘记在script标签之前添加jquery库。

You need to change your onSubmit attribute as follows 您需要按如下方式更改onSubmit属性

onsubmit="return submitform();"

So your html look like this 所以你的HTML看起来像这样

<form action="mail.php" method="post" onsubmit="return submitform();">
    <input type="text" id="name" name="name" placeholder="name">
    <input type="submit" value="submit">
</form>
<script type="text/javascript">
    function submitform(event) {
        if(document.getElementById('name').value=='') {
            alert('Please enter a name');
             event.preventDefault();
            return false;
        }
    }
</script>

<form action="mail.php" method="post" onsubmit="submitform(event);">
    <input type="text" id="name" name="name" placeholder="name">
    <input type="submit" value="submit">
</form>

You need to prevent default of submit. 您需要防止默认提交。 In JS return false does not stop the propagation of the "submit" function (with frameworks can be different). 在JS中,返回false不会阻止“ submit”功能的传播(使用不同的框架)。

I suggest you to read: event.preventDefault() vs. return false enter link description here 我建议您阅读: event.preventDefault()与返回false 在此处输入链接描述

just try this script 只要尝试这个脚本

function submitform() {
    var x = document.forms["fname"].value;
    x = x.trim(); // Remove white spaces
    if (x==null || x=="") {
        alert("First name must be filled out");
        return false;
    }
}

To cancel submission, the listener needs to return true or false. 要取消提交,侦听器需要返回true或false。 Also, if the function validates the fields, far better to name it for what it does rather than when it does it so call it something like "validateForm". 同样,如果函数验证了字段,则最好为其命名而不是在执行时对其进行命名,因此将其命名为“ validateForm”。

Also, giving a control a name of "name" masks the form's own name property. 另外,为控件提供“名称”的名称会掩盖表单自身的name属性。 While that doesn't matter here, in general it's not a good idea to give any form control a name that is the same as a standard property of a form (eg "submit" or "reset"). 尽管在这里无关紧要,但是通常不要给任何表单控件一个与表单的标准属性相同的名称(例如,“提交”或“重置”)。

So you might end up with something like: 因此,您可能会得到类似以下内容的结果:

<script>
    function validateForm(form) {
        if (form.personName.value == '') {
            alert('Please enter a name');
            return false;
        }
    }
</script>

<form ... onsubmit="return validateForm(this);">
    <input type="text" name="personName">
    <input type="submit">
</form>

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

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