简体   繁体   English

使用Jquery进行HTML5验证

[英]HTML5 Validation with Jquery

I want to make this compatible for browsers which do not support HTML5 validation. 我想使它与不支持HTML5验证的浏览器兼容。 Basically, all I am trying to do is give the basic error by default from the browser with HTML5 and also add a class of error to each input, label and parent div for each field that has an error which I will have some additional CSS effects such as red text/border. 基本上,我想做的就是在默认情况下使用HTML5从浏览器中提供基本错误,并为每个输入错误,标签错误和父div字段的错误添加一个错误类别,我将对此进行一些附加的CSS效果例如红色文字/边框。 What is happening is I get the default error message from the browser but no error classes are added to my input, label or div. 发生的事情是我从浏览器获取默认错误消息,但没有错误类添加到我的输入,标签或div中。 If I remove HTML5 required from one of the fields and fill out the rest of the form it will then run my function and add the error classes. 如果我从其中一个字段中删除了所需的HTML5并填写表单的其余部分,它将运行我的函数并添加错误类。 Below you will see my form and my jQuery function to add the error classes. 在下面,您将看到我的表单和添加错误类的jQuery函数。

What I think is happening is when HTML5 finds an error it is not running my submit function which would add the error classes. 我认为正在发生的事情是,当HTML5发现错误时,它没有运行将添加错误类的我的Submit函数。 I have figured I could trigger the invalid bind function for each input and add the error classes but I then don't think I would have any validation if the browser didn't support HTML5 validation. 我已经知道我可以为每个输入触发无效的绑定函数并添加错误类,但是如果浏览器不支持HTML5验证,我就不会进行任何验证。

jQuery portion: jQuery部分:

$('#contact_us_form').submit(function() {
    var error_count = 0;
    $('#contact_us_form div').each(function() {
        $(this).removeClass('error');
    });
    $('#contact_us_form input').each(function() {
        $(this).removeClass('error');
    }); 
    $('#contact_us_form label').each(function() {
        $(this).removeClass('error');
    });     
    if ($('#fname').val() == '') {
        error_count++;
        $('#fname').parent('div').addClass("error");
        $('#fname').addClass("error");
        $('label[for="fname"]').addClass("error");
    }
    if ($('#lname').val() == '') {
        error_count++;
        $('#lname').parent('div').addClass("error");
        $('#lname').addClass("error");
        $('label[for="lname"]').addClass("error");
    }       
    if ($('#email').val() == '') {
        error_count++;
        $('#email').parent('div').addClass("error");
        $('#email').addClass("error");
        $('label[for="email"]').addClass("error");
    } else if (!isValidEmailAddress( $('#email').val() ) ){
        error_count++;
        $('#email').parent('div').addClass("error");
        $('#email').addClass("error");
        $('label[for="email"]').addClass("error");
    }
    if ($('#phone').val() == '') {
        error_count++;
        $('#phone').parent('div').addClass("error");
        $('#phone').addClass("error");
        $('label[for="phone"]').addClass("error");
    } else if (!isPhone($('#phone').val())) {
        error_count++;
        $('#phone').parent('div').addClass("error");
        $('#phone').addClass("error");
        $('label[for="phone"]').addClass("error");
    }
    if ($('#message').val() == '') {
        error_count++;
        $('#message').parent('div').addClass("error");
        $('#message').addClass("error");
        $('label[for="message"]').addClass("error");
    }   

    if (error_count == 0) {
        submitFormWithAjax();
    } 

    return false;
});

HTML form: HTML形式:

<form class="form form_careers" action="#" id="contact_us_form">
    <fieldset>
        <legend>Contact Information</legend>
        <div class="field-name-first">
            <label for="fname" class="inlined">First Name</label>
            <input accesskey="f" class="input-text" id="fname" name="fname" required tabindex="1" type="text" value="" />
        </div>
        <div class="field-name-last">
            <label for="lname" class="inlined">Last Name</label>
            <input accesskey="l" class="input-text" id="lname" required tabindex="2" type="text" />
        </div>
        <div class="field-email-primary">
            <label for="email" class="inlined">Email</label>
            <input accesskey="e" class="input-text" id="email" required tabindex="3" type="email" />
        </div>
        <div class="field-phone-primary">
            <label for="phone" class="inlined">Phone</label>
            <input accesskey="p" class="input-text" id="phone" required tabindex="4" type="tel" />
        </div>
        <div class="field-message-comments">
            <label for="message" class="inlined">Message/Comments</label>
            <textarea accesskey="m" class="" id="message" required tabindex="5"></textarea>
        </div>
        <!--input class="cta_button cta_primary" tabindex="7" type="submit" value="Contact Us" /-->
        <button class="cta_button cta_primary" tabindex="6">Contact Us</button>
    </fieldset>
</form>

You could use a framework like modernizr to detect if the document supports the invalid event. 您可以使用modernizr之类的框架来检测文档是否支持invalid事件。 While initializing your app, check to see if invalid is a supported event. 在初始化您的应用程序时,请检查是否invalid是受支持的事件。 If so, bind to invalid , if not bind to submit . 如果是这样,则绑定到invalid ,如果不绑定,则submit It's a pretty robust framework (customizable and relatively small) for detecting support of various features. 这是一个非常健壮的框架(可定制且相对较小),用于检测对各种功能的支持。

This question might be of some help as well: How to bind to the submit event when HTML5 validation is used? 这个问题也可能会有帮助: 使用HTML5验证时如何绑定到Submit事件?

What you are looking for is a polyfill for HTML5 validation. 您正在寻找的是用于HTML5验证的polyfill。

This will basically, use javascript validation in cases where the clients browser is lacking. 基本上,在缺少客户端浏览器的情况下,将使用javascript验证。 Polyfills are implemented in cases where a browser doesn't support the feature, which leads toward a natural, and optimized web browsing experience for the user. 在浏览器不支持该功能的情况下会实现Polyfill,从而为用户带来自然而优化的Web浏览体验。

This is a good resource for HTML5 form polyfills: 对于HTML5表单polyfill,这是一个很好的资源:
https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#web-forms https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#web-forms

On polyfills: 在polyfills上:
http://en.wikipedia.org/wiki/Polyfill http://en.wikipedia.org/wiki/Polyfill

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

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