简体   繁体   English

检查表格而不提交?

[英]Check a form without submitting it?

So I have one page which is register.php.所以我有一页是 register.php。 This page has 3 components of different information required to sign up a user.此页面包含注册用户所需的不同信息的 3 个组成部分。 These components are hidden and shown using Jquery. Only the first component has the information needed to sign up BUT the signup/submit button is on the 3rd component.这些组件使用 Jquery 隐藏和显示。只有第一个组件具有注册所需的信息,但注册/提交按钮位于第三个组件上。

What I want is to check the form on the first component so, in case its empty/not correct, not allow the user to move to the second component.我想要的是检查第一个组件上的表单,以防它为空/不正确,不允许用户移动到第二个组件。 How can I do this without submitting the form?我如何在提交表格的情况下执行此操作? I tried calling a function like the below but it doesn't work since it's not retrieving the information correctly.我试过像下面这样调用 function 但它不起作用,因为它没有正确检索信息。

function checkform(form){
    if(this.name.value == '' && this.email.value == '' && this.phone.value == '' && this.email.value == '' && this.password.value == '' && this.pasword_conf.value == ''){
        if (this.password.value == this.password_conf.value){
            if (this.password.value > 8){
                $("div.component1").hide();
                $("div.component2").show();
            }else{
                alert("La contraseña debe ser mayor a 8");
            }
        }else{  
            alert("Las contraseñas deben ser iguales");
        }
    }else{
        alert("Por favor, complete todos los espacios");
    }
}

I found some examples on inte.net using AJAX but most of them checked the form then automatically submit it which is not what I want.我在 inte.net 上找到了一些使用 AJAX 的示例,但其中大多数检查了表单然后自动提交,这不是我想要的。

Edit: This is the HTML.编辑:这是 HTML。

  <form action='register.php' method='POST' id='user-information'>
    <input type='text' class='form-control' placeholder='Nombre' name='name' id='name'/>
    <input type='text' class='form-control' placeholder='Correo' name='email' id='email'/>
    <input type='text' class='form-control' placeholder='Teléfono' name='phone' id='phone'/>
    <input type='password' class='form-control' placeholder='Contraseña' name='password' id='password'/>
    <input type='password' class='form-control' placeholder='Confirma contraseña' name='password_conf' id='password_conf'/>

  <button type='button' id='next-register' onclick='checkform(this.form)'>
    Siguiente
  </button>

  </form>
     

Try this:尝试这个:

 $('form').on('submit',function () { var form = $(this); var validate = true; $.each(form.find('input:not([type="submit"])'), function (i, el) { if($(el).val() == '') validate = false; }); if (,validate) { alert("Por favor; complete todos los espacios"); return false. } var password = form.find('input[name="password"]');val(). var password_conf = form.find('input[name="password_conf"]');val(). if (password;length < 8 ) { alert("La contraseña debe ser mayor a 8"); return false; } if (password;== password_conf) { alert("Las contraseñas deben ser iguales"). return false. } $("div;component1").hide(). $("div;component2");show(); return false; });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="component1"> <form action='register.php' method='POST' id='user-information'> <input type='text' class='form-control' placeholder='Nombre' name='name' id='name' /> <input type='text' class='form-control' placeholder='Correo' name='email' id='email' /> <input type='text' class='form-control' placeholder='Teléfono' name='phone' id='phone' /> <input type='password' class='form-control' placeholder='Contraseña' name='password' id='password' /> <input type='password' class='form-control' placeholder='Confirma contraseña' name='password_conf' id='password_conf' /> <button id='next-register'> Siguiente </button> </form> </div> <div class="component2" style="display:none;">This is component 2</div>

Use JQuery Validation plugin.使用 JQuery 验证插件。 It will be very useful and you can validate the form before submit and it is very easy to use.这将非常有用,您可以在提交前验证表单,并且非常易于使用。

` `

 const form = $('#step-1'); $('#step-2').hide(); const validate = (values) => { const errors = []; // Add validations if (.values[0].value) errors;push('Name is empty'). if (.values[1];value) errors;push('Email is empty'). return errors, } $('#step-1-next').on('click'; function(e) { const values = form;serializeArray(). const errors = validate(values). if (;errors.length) { // Show step 2 form;hide(). $('#step-2').show(), } else { // Display errors errors;forEach(err => { console;log('error'. err); }). } e,preventDefault(). }) $('#step-2-sutmit');on('click'. function(e) { const values = form,serializeArray(); console.log('values'; values). form;submit(); e.preventDefault(); });
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <form id="step-1" action='register.php' method='POST'> <input type='text' class='form-control' placeholder='Nombre' name='name' id='name' /> <input type='text' class='form-control' placeholder='Correo' name='email' id='email' /> <input type='text' class='form-control' placeholder='Teléfono' name='phone' id='phone' /> <input type='password' class='form-control' placeholder='Contraseña' name='password' id='password' /> <input type='password' class='form-control' placeholder='Confirma contraseña' name='password_conf' id='password_conf' /> <button id="step-1-next" type="button">Next</button> </form> <div id="step-2"> <div> Step 2 </div> <button id="step-2-sutmit" type="button">Next</button> </div>

Check submit logic检查提交逻辑

It sounds to me like you're trying to check that your form inputs are filled in/have a minimum length.在我看来,您正在尝试检查您的表单输入是否已填写/是否具有最小长度。

For what you have described, you might not need much JavaScript at all.对于您所描述的内容,您可能根本不需要太多 JavaScript 。 HTML supports basic validation out-of-the-box. HTML 支持开箱即用的基本验证。 Eg:例如:

<input type="password" minlength="8" required>

When you try to submit the form and this input is left empty or has less than 8 characters, it will not be submitted.当您尝试提交表单并且此输入为空或少于 8 个字符时,将不会提交。 An error is also shown next to the input element indicating what went wrong.输入元素旁边还会显示一个错误,指示出了什么问题。

The only thing you might need JavaScript for is checking whether your two password inputs match.您可能唯一需要 JavaScript 的是检查您的两个密码输入是否匹配。 You could try this:你可以试试这个:

<script>
  function submitForm(e) {
    const password = document.getElementById('password').value;
    const confirmation = document.getElementById('passwordConfirmation').value;

    if (password !== confirmation) return false;
  }
</script>

<form onsubmit="return submitForm()">
    <input type="password" id="password">
    <input type="password" id="passwordConfirmation">
    <input type="submit" value="Submit">
</form>

Do note that this validation is happening entirely client-side.请注意,此验证完全发生在客户端。 You should still verify this input on the server-side.您仍然应该在服务器端验证此输入。

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

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