简体   繁体   English

如何在HTML5中使输入字​​段为“必填”?

[英]How To Make an INPUT Field As “Required” In HTML5?

On my website I have a reservation forum, and I put that the person cannot submit the forum unless submitting their name and phone number. 在我的网站上,我有一个预订论坛,我认为除非提交其姓名和电话号码,否则该人不能提交该论坛。

<p><label>Phone #:<input type="text" id="phone" name="phone" placeholder="###-###-####" style="margin:10px; width:100px; height:15px" required>

It works perfect on every device besides any mobile device using safari, so I wanted to know what code can I use so it can work on all devices. 除了使用safari的任何移动设备外,它在所有设备上均可完美运行,因此我想知道我可以使用什么代码,使其可以在所有设备上使用。

Any input/help/advice would be appreciated. 任何输入/帮助/建议,将不胜感激。

Mobile Safari validation isn't really up to par compared to all the other browsers. 与所有其他浏览器相比,移动Safari验证确实没有达到标准。 You can check out feature support by going to Can I use . 您可以转到可以使用来检查功能支持。 What I would do to fix this issue is grab Modernizr.js or use some other feature detection to find out if form validation is supported. 为了解决这个问题,我要做的是抓住Modernizr.js或使用其他功能检测来确定是否支持表单验证。 If it isn't, then just put it in a little JS snippet saying that the field is required. 如果不是,则将其放在一个小JS片段中,说明该字段是必填字段。

Something like this should do if you use Modernizr.js and select "Input Attributes" under HTML5. 如果您使用Modernizr.js并在HTML5下选择“输入属性”,则应该执行类似的操作。

var jsRequired;

if (!Modernizr.input.required) {
    jsRequired = true;
}

Then on the form submission: 然后在表单提交上:

$('#form-submit').on('click', (function(evt) { // this is the form submit button
    evt.preventDefault(); // preventing reload of the page

    if (jsRequired == true) {
        var requiredInputs = $('input[required]');

        for (var i = 0; i < requiredInputs.length; i++) {
            if (requiredInputs[i].value == '') {
                $('#submission-info').text('Whoa whoa whoa, you missed one...');

                // highlight the missed one by adding a class
                requiredInputs[i].className += " submission-error";
                return false; //; stop the rest of the submission
            }
        }
    }

    var formData = $('form#contact-form').serialize(); // serialize data

    $.ajax({
        url: 'mail.php', // rename this to your php file
        type: 'POST',
        data: formData
    }).done(function(response) {
        // do stuff to to show the user that the form was submitted
        $('#submission-info').text('Success, your information has been sent to us (and the NSA of course) and we will reply to you as soon as possible.');
    }).fail(function(response, error) {
        // tell the user what happened that caused the form submission to fail
        $('#submission-info').text('Oh no, something happened. Maybe try again?');
    });
});

Some of the code should be changed around to fit your stuff but otherwise it should be in the general direction of what you are looking to do. 某些代码应随处更改以适合您的内容,否则应朝您要执行的一般方向发展。

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

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