简体   繁体   English

电子邮件地址和电话验证| jQuery的

[英]Email Address and Phone Validation | jQuery

Having some trouble getting validation to work for Email and Phone on an input field. 在输入字段上使验证无法用于电子邮件和电话时遇到一些麻烦。

Basically I'm after validation on the email input when it is selected. 基本上,我在选择电子邮件输入后就对其进行了验证。 Phone validation - mainly to check for numbers used. 电话验证-主要是检查所使用的号码。

I found jQuery Validate and referenced that in my demo. 我找到了jQuery Validate并在我的演示中引用了它。

This is my first time to Reg Ex Validation and got a little confused so was hoping someone could help me on that front. 这是我第一次进行Reg Ex验证,有些困惑,所以希望有人可以在这方面为我提供帮助。

The phone validation is working however, I can't seem to figure out how to meet the requirements of the field when testing. 电话验证正在运行,但是,我似乎无法弄清楚如何在测试时满足现场要求。 It's also asking email as a required field. 它还要求将电子邮件作为必填字段。

Ultimately, a user has to input either a valid email or phone number so validation or error handling will need to be done for that too which I can do. 最终,用户必须输入有效的电子邮件或电话号码,因此也需要为此进行验证或错误处理。

DEMO HERE 此处演示

Here is my jQuery: 这是我的jQuery:

var ebuForm = {

        init : function() {
            ebuForm.showInput();
            ebuForm.validatePhone();
        },

        showInput : function(e) {

            var radioInput = $("input[type='radio']"),
                emailRadio = $("input[value='email']");

            radioInput.change(function(){

                var emailInput = $('.email-input'),
                    phoneInput = $('.phone-input');

                if($(this).val() =="email") {
                    emailInput.show();
                    phoneInput.hide();
                    console.log('Email Enabled');
                } else {
                    emailInput.hide();
                    phoneInput.show();
                    console.log('Phone Enabled');
                }

            });
            emailRadio.prop('checked', true).trigger("change");
        },

        validatePhone : function() {
            $('#myform').validate({
                rules: {
                    phone: {
                        phoneUS: true,
                        required: true
                    }
                },
                submitHandler: function(form) { // for demo
                    alert('valid form');
                    return false;
                }
            });

        }

};
$(function() {
    ebuForm.init();
});

Working Fiddle Example: http://jsfiddle.net/775X2/56/ 工作小提琴示例: http : //jsfiddle.net/775X2/56/

Your HTML: 您的HTML:

<form id="myform">
    <div class="grid4">
        <input type="radio" checked="checked" tabindex="50" class="left" name="x" id="email" value="email" />
        <label for="email" class="left marginRight20">Email</label>
        <input type="radio" tabindex="60" class="left" name="x" id="phone" />
        <label for="phone" class="left">Phone</label>
        <br />
        <input type="text" class="email-input" name="emailer" placeholder="Email Address" />
        <input type="text" class="phone-input" name="phone" placeholder="Phone Number" />
    </div>
</form>
<a href="http://docs.jquery.com/Plugins/Validation" target="_blank">Validation Documentation</a>

Some jQuery: 一些jQuery的:

var ebuForm = {

    init: function () {
        ebuForm.showInput();
        ebuForm.validatePhone();
    },

    showInput: function (e) {

        var radioInput = $("input[type='radio']"),
            emailRadio = $("input[value='email']");

        radioInput.change(function () {

            var emailInput = $('.email-input'),
                phoneInput = $('.phone-input');

            if ($(this).val() == "email") {
                emailInput.show();
                phoneInput.hide();
                console.log('Email Enabled');
            } else {
                emailInput.hide();
                phoneInput.show();
                console.log('Phone Enabled');
            }

        });
        emailRadio.prop('checked', true).trigger("change");
    },

    validatePhone: function () {
        $('#myform').validate({
            rules: {
                phone: {
                    phoneUS: true,
                    required: true
                },
                emailer: {
                    required: true,
                    email: true
                },
            },
            submitHandler: function (form) { // for demo
                alert('valid form');
                return false;
            }
        });

    }

};
$(function () {
    ebuForm.init();
});

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

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