Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.test() or .match() - JQuery

Status
Not open for further replies.

JRB-Bldr

Programmer
May 17, 2001
3,281
US
I am trying to get the .match() or .test() function to work with a 'Regular Expression'

Through some web searches for Date Validation, I found the Regular Expression to use for MMDDYYYY Date Validation:
//match date in format MM/DD/YYYY
var dateMMDDYYYYRegex = '^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$';

And those same searches say to use this Regular Expression in a .match() or .test() to validate the Date

For preliminary testing, I have tried:
What is your date of birth?
<input type="text" id="input-dob" class="medium" placeholder="MM/DD/YYYY" name="lead-dob"/><label for="lead-dob" style="padding-left:8px">MM/DD/YYYY</label>


Then in the associated bound FocusOut code I have
var dateMMDDYYYYRegex = '^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$';
var chkDate = $(event.currentTarget).val()
// The chkDate value is confirmed via an alert() to be good here

// But when I try to check this value...
alert(dateMMDDYYYYRegex.test(chkDate));

I get a Firebug error message dateMMDDYYYYRegex.test is not a function

And I tried
var dateMMDDYYYYRegex = '^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$';
var chkDate = $(event.currentTarget).val()
// The chkDate value is confirmed via an alert() to be good here

// But when I try to check this value...
alert(chkDate.match(dateMMDDYYYYRegex));

I get a Firebug error message chkDate.match is not a function

Any suggestions and/or advice as to what I am doing wrong?

Thanks,
JRB-Bldr
 
Hi

That is the reverse mistake of what Randy did in thread216-1696707.

You assigned to dateMMDDYYYYRegex a [tt]String[/tt] value then tried to call a [tt]RegExp[/tt] method on it.

Assign it a [tt]RegExp[/tt] value instead :
JavaScript:
[b]var[/b] dateMMDDYYYYRegex [teal]=[/teal] [fuchsia]/^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d$/[/fuchsia][teal];[/teal]

( Please choose a TGML tag which enforces monospaced font on your code : [tt][ignore]
Code:
[/ignore][/tt]..[tt][ignore]
[/ignore][/tt], [tt][ignore][pre][/ignore][/tt]..[tt][ignore][/pre][/ignore][/tt] or [tt][ignore][tt][/ignore][/tt]..[tt][ignore][/tt][/ignore][/tt]. That bold looks horrible. )

Feherke.
[link feherke.github.com/][/url]
 
test is a function of a regexp object. Keyword: Object. You are passing it a string as your expression is enquoted.
As Feherke points out remove the quotes to turn it into a valid Regexp object.

Alternatively if you want to be more specific, you can use the Regexp object constructor as such:

Code:
[b][COLOR=#0000FF]var[/color][/b] dateMMDDYYYYRegex [COLOR=#990000]=[/color] [b][COLOR=#0000FF]new[/color][/b]  [b][COLOR=#000000]RegExp[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[/color][COLOR=#CC33CC]\d\d[/color][COLOR=#FF0000]$'[/color][COLOR=#990000]);[/color]



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks for both Feherke & Phil AKA Vacunita

While I don't have a clue as to how to interpret what means what within the 'Regular Expression' itself, what you gave me got things working.

I actually ended up going to:

Where I found a somewhat different Regular Expression string for my Date Validation, but the basics of how to use it which I got from you was what made it work.

Thanks,
JRB-Bldr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top