SoftwareSavant
SoftwareSavant

Reputation: 9737

Can't get regexes to work in jquery

I am using asp.net MVC2 and I am attepmting to add regexes to do some forms for some validation... That being said, none of them are working at all. First thing I did was attempt to do a simple .blur function call. That didn't work at all...

$('#DischargeDateTimeMask').blur(function () {            
        var inputVal = $(this).val();
        var dateReg = /^(1[012]|0[1-9]):[0-5][0-9](\\s)? (am|pm)$/;
        if (!characterReg.test(inputVal)) {
            //$(this).after('<span class="error error-keyup-4">Incorrect time format fool!: HH:mm am|pm</span>');
        alert(dateReg.test(input));
        }
    });​

I just wanted to test to see if my regex was catching anything, and apparently it is throwing some kind of error. So I said screw that and I decide to try and add an extra method to the Jquery Validator... My failure is document below...

$.validator.addMethod(
        "time",
        function(value, element){
            /^(1[012]|0[1-9]):[0-5][0-9](\\s)? (am|pm)$/.test(value);
        },
        "The time format is invalid"
    );
$("#temp1").validate({
        rules: {
DischargeDateTimeMask: {
                time: true,
                required: false
            }
        },
        messages: {
        }
    });
});

Ignore some of the rest of the code... I have other validation rules in there that do work (when I get rid of this code) but are unrelated. What on earth am I doing wrong? It seems as though everything is on the up and up here? Am I missing something simple?

Upvotes: 0

Views: 139

Answers (2)

Ryley
Ryley

Reputation: 21226

First off, you're going to want to have your method check whether required is set or not, otherwise it will always check your regex, even when required: false is also set. So the first line of your new method should be this:

if (this.optional(element)){ return true;}

And as other people pointed out, you need to return the result. I also think your regular expression is a bit off with the spaces part, I would just make it \s? unless I'm missing something.

So you're total function would look like this:

$.validator.addMethod(
    "time",
    function(value, element){
        if (this.optional(element)){ return true;}

        return /^(1[012]|0[1-9]):[0-5][0-9](\\s)? (am|pm)$/.test(value);
    },
    "The time format is invalid"
);

Here it is working: http://jsfiddle.net/ryleyb/w7EAV/

Upvotes: 1

SoftwareSavant
SoftwareSavant

Reputation: 9737

Aparently...

$('#DischargeDateTimeMask').blur(function () {
        var inputVal = $(this).val();
        var dateReg = /^(1[012]|0[1-9]):[0-5][0-9](\\s)? (am|pm)+$/;
        if (!characterReg.test(inputVal)) {
            $(this).after('<span class="error error-keyup-4">Incorrect time format fool!: HH:mm am|pm</span>');
        }
        //alert(dateReg.test(inputVal));
    });

This works but what I had before does not. It looks exactly like what I had before but whatever. But with that experience under my belt I decided to try and get the validation working... Apparently what I had worked before, I just had an error somewhere else in my code. Thanks for the help guys.

Upvotes: 0

Related Questions