简体   繁体   English

如何使用onClick JQuery函数运行HTML5验证?

[英]How to run HTML5 Validation with onClick JQuery function?

I'm trying to implement HTML5 Validation in my project. 我正在尝试在我的项目中实现HTML5验证。 Seems very helpful and easy for use. 似乎非常有帮助且易于使用。 However this validation will work only if input type is set to submit . 但是,仅当输入类型设置为submit此验证才有效。 That part is causing problem for me. 那部分给我造成了麻烦。 I have multiple forms and I use same name attribute on all Submit buttons. 我有多种形式,并且在所有“提交”按钮上使用相同的name属性。 So my function looks like this: 所以我的函数看起来像这样:

HTML: HTML:

<form name="myForm" id="myForm" method="POST" action="#">
    <fieldset>
        <legend>User Info</legend>
        <div class="formItem">
            <label for="last_name">Last Name:</label>
            <input type="text" name="frmst_lname" id="frmst_lname" value="" size="20" maxlength="30" title="Maximum length 30 characters." required/>
        </div>
        <div class="formItem">
            <label for="first_name">First Name:</label>
            <input type="text" name="frmst_fname" id="frmst_fname" value="" size="20" maxlength="30" title="Maximum length 30 characters." required/>
        </div>
        //Same button I have on the other forms. Only ID is different.
        <div class="formItem">
            <p align="center"><input type="button" name="frmSubmit" id="frmstSubmit" value="Submit"></p>
        </div>
    </fieldset>
</form>

JQuery: jQuery的:

$('input[name="frmSubmit"]').on('click', function(){
    var frmID = $(this).closest('form').prop('id'); //take form ID
    var processVal = $(this).attr('data-process'); //which process will be executed Add or Edit
    //Here I would like to check form validation then process AJAX call
});

So far I couldn't get HTML 5 validation to work with onClick function. 到目前为止,我无法通过onClick函数使用HTML 5验证。 I tried to use checkValidty() but message never showed up for required fields on my form. 我尝试使用checkValidty()但是我的表单上的必填字段始终没有显示消息。 I'm wondering if HTML5 is good fit for my project and how I can get validation to work with onClick function? 我想知道HTML5是否适合我的项目,如何使验证与onClick函数一起使用? If anyone have solution for this problem please let me know. 如果有人有解决此问题的方法,请告诉我。 Thanks in advance! 提前致谢!

If you want HTML5 Validation to work change button type to submit. 如果您希望HTML5验证起作用,请更改按钮类型以提交。

<input type="submit" name="frmSubmit" id="frmstSubmit" value="Submit">

If you want to do some custom validation add a onsubmit attribute. 如果要进行一些自定义验证,请添加onsubmit属性。

<form method="POST" action="form-handler" onsubmit="return checkForm(this);">
<p>Input: <input type="text" size="32" name="inputfield">
<input type="submit"></p>
</form>

<script type="text/javascript">

  function checkForm(form)
  {
    if(!condition1) {
       alert("Error: error message");
       form.fieldname.focus();
       return false;
    }
    if(!condition2) {
       alert("Error: error message");
       form.fieldname.focus();
       return false;
    }
    ...
    return true;
  }

</script>

see: http://www.the-art-of-web.com/javascript/validate/ 请参阅: http : //www.the-art-of-web.com/javascript/validate/

If you want to use an AJAX call with HTML5 validation try using e.preventDefault() in a on submit function. 如果您想通过HTML5验证使用AJAX调用,请尝试在on Submit函数中使用e.preventDefault()

$('#myForm').on('submit',function(e) {
    e.preventDefault();

    $.ajax({
        url: url,
        type: 'post',
        data: data,
        success: function (data) {
            if (data.success) {

            } else {

            }
        }
   });
})

or 要么

$(document).on('submit','#myForm',function(e) {
  e.preventDefault();

  $.ajax({
        url: url,
        type: 'post',
        data: data,
        success: function (data) {
            if (data.success) {

            } else {

            }
        }
   });
})

Update - added to answer question in comment: 更新-添加以回答评论中的问题:

See JSFiddle to use one on change function for multiple forms and get id attribute. 请参阅JSFiddle以将一个on change函数用于多种形式并获取id属性。

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

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