简体   繁体   English

提交之前进行表单验证的最佳方法是什么

[英]What is the best way to do form validation before submitting

Please some one suggest me on, 请有人建议我

What is the best way to do form validation before submitting? 提交之前进行表单验证的最佳方法是什么?

Actual scenario is like, i have a button called save,so when user press save button. 实际情况就像,我有一个名为“保存”的按钮,因此当用户按下“保存”按钮时。

I need to validate the data and pass the flow to server to store the data in the tables. 我需要验证数据并将流传递到服务器以将数据存储在表中。

Instead of doing form data validation in server side, is there any possible way to check those in client side itself 除了在服务器端进行表单数据验证之外,还有其他任何方法可以在客户端本身中进行检查

 <form> <header> <h1>Testing </h1> </header> <p> Receipt number: <input type="text" id="grn" class="tb1" onkeypress="return isNumber(event)" /> Type <select name="evalu" id="evalu"> <option value="electrical">Electrical</option> <option value="mechanical">Mechanical</option> </select> cad <select name="cd" id="cd"> <option value="unit1">xv</option> <option value="unit2">ed</option> </select> <input type="button" id="find" value="Find" class="button0" /> <br> <br> Report No <input type="text" name="irepno" id="irepno" class="tb1" maxlength="8" /> date <input type="text" name="idt" id="idt" class="tb1" value="<%= new SimpleDateFormat(" dd-MM-yyyy ").format(new java.util.Date())%>"> <input type="button" id="search" value="Search" class="button0" /> <br></br> <input type="button" value="Save the record" id="saverecord" class="button0"> </p> </form> 

Javascript itself is developed with an intention to add client side processing of data and validations. 开发Javascript本身的目的是增加客户端对数据的处理和验证。

The best way depends on the situation where you are applying and also the javascript technologies. 最好的方法取决于您所应用的情况以及javascript技术。

If you are not using any specific client side technologies or frameworks for example angularjs or emberjs etc. 如果您未使用任何特定的客户端技术或框架,例如angularjs或emberjs等。

You can try using jquery validation plugin which is avialable ate https://jqueryvalidation.org/ 您可以尝试使用https://jqueryvalidation.org/提供的 jquery验证插件。

 $(function() { // Initialize form validation on the registration form. // It has the name attribute "registration" $("form[name='registration']").validate({ // Specify validation rules rules: { // The key name on the left side is the name attribute // of an input field. Validation rules are defined // on the right side firstname: "required", lastname: "required", email: { required: true, // Specify that email should be validated // by the built-in "email" rule email: true }, password: { required: true, minlength: 5 } }, // Specify validation error messages messages: { firstname: "Please enter your firstname", lastname: "Please enter your lastname", password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long" }, email: "Please enter a valid email address" }, // Make sure the form is submitted to the destination defined // in the "action" attribute of the form when valid submitHandler: function(form) { form.submit(); } }); }); 
 label, input { display: block; } input{ margin-bottom:15px; } label.error { color: red; margin-top:-10px; margin-bottom:15px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script> <div class="container"> <h2>Registration</h2> <form action="" name="registration"> <label for="firstname">First Name</label> <input type="text" name="firstname" id="firstname" placeholder="John" /> <label for="lastname">Last Name</label> <input type="text" name="lastname" id="lastname" placeholder="Doe" /> <label for="email">Email</label> <input type="email" name="email" id="email" placeholder="john@doe.com" /> <label for="password">Password</label> <input type="password" name="password" id="password" placeholder="&#9679;&#9679;&#9679;&#9679;&#9679;" /> <button type="submit">Register</button> </form> </div> 

There are many ways to validate a form. 有很多方法可以验证表单。 I prefer validating a form using HTML elements which is a quick way to check the input details. 我更喜欢使用HTML元素验证表单,这是检查输入详细信息的快速方法。

Here is a code snippet I used to validate details entered by the client in a simple form. 这是我用来验证客户端以简单形式输入的详细信息的代码段。

<fieldset>
    <legend>Enter Your Details</legend>
    <p>
        <label for="fave"> Mobile:
            <input maxlength="10" autofocus="on" autocomplete="on" name="mobile" placeholder="Mobile number"/>
        </label>
    </p>

    <p>
        <label for="name"> Name:
            <input maxlength="30" pattern="^.* .*$" required size="15" name="name" placeholder="Your name"/>
        </label>
    </p>

    <p>
        <label for="password"> Password:
            <input type="password" required name="password" placeholder="Min 6 characters"/>
        </label>
    </p>

    <p>
        <label for="email">
            Email: <input type="email" pattern=".*@mydomain.com$" placeholder="user@domain.com" id="email" name="email"/>
        </label>
    </p>
    <p>
        <label for="tel">
            Tel: <input type="tel" placeholder="(XXX)-XXX-XXXX" id="tel" name="tel"/>
        </label>
    </p>
    <p>
        <label for="url">
            Your homepage: <input type="url" id="url" name="url"/>
        </label>
    </p>
</fieldset>

Few elements like type , maxlength , pattern , required , size are used for validating a form in client side. 很少使用typemaxlengthpatternrequiredsize之 类的元素来在客户端验证表单。

I like the book The Definitive Guide to HTML5 , where you can learn to validate a form using front-end development. 我喜欢《 HTML5权威指南》这本书,在这里您可以学习使用前端开发来验证表单。

Hope this solves your problem. 希望这能解决您的问题。

On form submit, write javascript or jquery script to validate and pass form values to your servlets. 在提交表单时,编写JavaScript或jquery脚本以验证表单值并将其传递给servlet。

you can use this jquery plugin too. 您也可以使用此jquery插件

There are some great validation libraries out there. 有一些很棒的验证库。 One I like in particular is jQuery.validate.js as it is well documented and easy to use. 我特别喜欢的一个是jQuery.validate.js ,它有据可查且易于使用。

If you would prefer to write your own, a good place to start would be this W3Schools article on Javascript form validation 如果您希望自己编写,那么一个很好的起点是有关Java表单验证的W3Schools文章。

I suggest you to options, you can choose yourself: 我建议您选择,您可以选择自己:

1) Write your validate code inside the function when you click saverecord button. 1)单击保存记录按钮时,将验证代码写入函数中。

2) Validate input field (in your case I guess that "Receipt number" and "Report No" is only number), you can write function to handle onkeypress ( or onchange) event to validate which typing from users. 2)验证输入字段(在您的情况下,我想“收据编号”和“报告编号”仅是编号),您可以编写函数来处理onkeypress(或onchange)事件,以验证用户输入的内容。

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

相关问题 在提交之前在javascript中检查django表单错误的最佳方法是什么? - what is the best way to check django form errors in javascript before submitting? 提交用于JavaScript验证的表单时,函数调用之前的返回结果是什么? - When submitting a form for JavaScript validation what does the return before the function call do? 验证之前提交JQuery表单 - Jquery form submitting before validation 在提交之前通过 Javascript 进行一些表单验证是否明智? - Is it smart to do some form validation through Javascript before submitting? 自定义jQuery表单验证-最佳方法是什么? - Custom jQuery Form Validation - What Is The Best Way? javascript 表单验证,在提交帮助之前? - javascript form validation, before submitting help? 表单验证正则表达式和DOM:什么是在表单验证中实现正则表达式的最佳方法 - Form validation regex and DOM: What would be the best way to implement regex in a form validation ASP.NET-在提交表单之前调用javascript验证功能 - ASP.NET - Call javascript validation function before submitting form 在 JSF 中提交表单之前使用 JS 进行客户端验证 - Using JS for client side validation before submitting form in JSF 提交前使用Ajax上传文件的最佳方法 - Best way to upload files using Ajax and before submitting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM