jQuery验证不适用于所有字段

时间:2012-06-12 11:41:44

标签: html css validation jquery

请参见此处: http://jsfiddle.net/zYcJm/

jQuery应该验证每个字段以检查它们是否包含文本以及电子邮件是否有效。但是:

  • 对邮件的验证(id =邮件)似乎不起作用,如果您在邮件框中没有任何内容的情况下单击发送,则表示您没有收到错误,但您应该这样做。
  • 如果输入无效输入并单击“发送”然后更改输入以使其有效,则“发送”按钮仍然无法点击。

我的代码中的问题在哪里?

2 个答案:

答案 0 :(得分:1)

textarea

中留下空格

<textarea name="message" placeholder="your message here" id="message" class="message"> </textarea>

尝试解决这个问题,应该解决问题。

答案 1 :(得分:0)

// JavaScript Document
$(document).ready(function() {

    $('#contactForm #submit').click(function() {
        // Fade in the progress bar
        $('#contactForm #formProgress').hide();
        $('#contactForm #formProgress').html('<img src="http://dl.dropbox.com/u/16640285/ajax-loader.gif" /> Sending&hellip;');
        $('#contactForm #formProgress').fadeIn();

        // Disable the submit button
        $('#contactForm #submit').attr("disabled", "disabled");

        // Clear and hide any error messages
        $('#contactForm .formError').html('');

        // Set temaprary variables for the script
        var isFocus = 0;
        var isError = 0;

        // Get the data from the form
        //    var name=$('#contactForm #name').val();
        var email = $('#contactForm #email').val();
        //    var subject=$('#contactForm #subject').val();
        var message = $('message').val();

        // Validate the data
        //    if(name=='') {
        //    $('#contactForm #errorName').html('This is a required field.');
        //        $('#contactForm #name').focus();
        //        isFocus=1;
        //        isError=1;
        //    }
        if (email == '') {

            $('#contactForm #errorEmail').html('Please enter your email address.');
            if (isFocus == 0) {
                $('#contactForm #email').focus();
                isFocus = 1;
            }
            isError = 1;
        } else {
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            if (reg.test(email) == false) {
                $('#contactForm #errorEmail').html('Please enter a valid email address.');
                if (isFocus == 0) {
                    $('#contactForm #email').focus();
                    isFocus = 1;
                }
                isError = 1;
            }
        }

        if (message ==  null || message == '') {

            $('#contactForm #errorMessage').html('This is a required field.');
            if (isFocus == 0) {
                $('#contactForm #message').focus();
                isFocus = 1;
            }
            isError = 1;
        }

        // Terminate the script if an error is found
        if (isError == 1) {
            $('#contactForm #formProgress').html('');
            $('#contactForm #formProgress').hide();

            // Activate the submit button
            $('#contactForm #submit').attr("disabled", "");

            return false;
        }

        $.ajaxSetup({
            cache: false
        });

        var dataString = //'name='+ name + '&
        'email=' + email + //'&subject=' + subject + 
        '&message=' + message;
        $.ajax({
            type: "POST",
            url: "http://dl.dropbox.com/u/16640285/submit-form-ajax.php",
            data: dataString,
            success: function(msg) {

                //alert(msg);
                // Check to see if the mail was successfully sent
                if (msg == 'Mail sent') {
                    // Update the progress bar
                    $('#contactForm #formProgress').html('<img src="http://dl.dropbox.com/u/16640285/ajax-complete.gif" /> Message sent.').delay(2000).fadeOut(400);

                    // Clear the subject field and message textbox
                    //    $('#contactForm #subject').val('');
                    $('#contactForm #message').val('');
                } else {
                    $('#contactForm #formProgress').html('');
                    alert('There was an error sending your email. Please try again.');
                }

                // Activate the submit button
                $('#contactForm #submit').attr("disabled", "");
            },
            error: function(ob, errStr) {
                $('#contactForm #formProgress').html('');
                alert('There was an error sending your email. Please try again.');

                // Activate the submit button
                $('#contactForm #submit').attr("disabled", "");
            }
        });

        return false;
    });
});​

粘贴这个并检查buddyyyyyyyy