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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax Error: Unexpected token { after a regular expression test 2

Status
Not open for further replies.

Katerine

Programmer
Mar 9, 2001
234
US
Hi,
I have what I hope is a simple question. :) The chrome debugger is giving me an error in my validation code:
Uncaught SyntaxError: Unexpected token {

The error is on the "{" line in the below code:

JavaScript:
  if ((document.forms.regform.graddate.value.length > 0) && ( !/^\d{2}\/\d{2}$/.test(document.forms.regform.graddate.value) ) //graddate mm/yy
  {
    alert("Graduation date must be in format mm/yy to continue.");
    return false;
  }

Any simple fix? Many thanks!

Katie
 
Oh, FYI, I just tried replacing the above code with this:

JavaScript:
 //graddate mm/yy
 var patt = new RegExp("/^\d{2}\/\d{2}$/");
 if ((document.forms.regform.graddate.value.length > 0) && ( !patt.test(document.forms.regform.graddate.value) )
  {
    alert("Graduation date must be in format mm/yy to continue.");
    return false;
  }

No dice. Same error. I know it must be something simple; I'm just not seeing it. :
Many thanks again!

Katie
 
Hi

Missing closing parenthesis ( [tt])[/tt] ) :
JavaScript:
[gray]//[/gray] [red],------------------------------------------------------------- 1 ---------------------------------------------------------???[/red]
[gray]//[/gray] [red]|[/red][green],---------------------- 2 -----------------------.    ,------------------------------ 2 ------------------------------.[/green]
[gray]//[/gray] [red]|[/red][green]|                                                |    |[/green]                       [blue],----------------- 3 ----------------.[/blue]  [green]|[/green]
[gray]//[/gray] [red]|[/red][green]|                                                |    |[/green]                       [blue]|                                     |[/blue] [green]|[/green]
[b]if[/b] [teal](([/teal]document[teal].[/teal]forms[teal].[/teal]regform[teal].[/teal]graddate[teal].[/teal]value[teal].[/teal]length [teal]>[/teal] [purple]0[/purple][teal]) && ( ![/teal][fuchsia]/^\d{2}\/\d{2}$/[/fuchsia][teal].[/teal][COLOR=#FF6600]test[/color][teal]([/teal]document[teal].[/teal]forms[teal].[/teal]regform[teal].[/teal]graddate[teal].[/teal]value[teal]) )[/teal] [gray]//graddate mm/yy[/gray]
  [teal]{[/teal]
    [COLOR=#FF6600]alert[/color][teal]([/teal][i][green]"Graduation date must be in format mm/yy to continue."[/green][/i][teal]);[/teal]
    [b]return false[/b][teal];[/teal]
  [teal]}[/teal]

Feherke.
feherke.ga
 
Huh. You are right. Thank you.

(I say, "huh," because that's the first thing I checked for. And the second, and the third... and somehow, I counted the parentheses wrong every time. Even after you counted them for me, it took me a while to see it. I'm sick, and my mind is fuzzy. I'm sticking with that story. :) )

Thanks again!

Katie
 
Hi

A hint : try other browsers too. Personally I find Chrome's message abit unclear.
FireBug Console said:
[tt][COLOR=white red] X [/color] [red]SyntaxError: missing ) after condition[/red]
[green]{[/green]
[green][/green][/tt]
Opera Dragonfly Console said:
[tt]
[navy]if[/navy] (([navy]document[/navy].[navy]forms[/navy].regform.graddate.value.length > [red]0[/red]) && ( ![green]/^\d{2}\/\d{2}$/[/green].test([navy]document[/navy].[navy]forms[/navy].regform.graddate.value) ) [gray]//graddate mm/yy[/gray]
[COLOR=white darkred]{[/color]
[bbox]
[tt]Syntax error at line 23 while loading: expected ')', got '{'
  {
--^
[/tt]​
[/bbox]
[/tt]

Feherke.
feherke.ga
 
Incidentally, you might want to change this:

Code:
d{2}

to this:

Code:
d{1,2}

That would allow both single and double digits to be entered - which as a user, I'd find a far nicer experience (why force someone to enter "02" when they can enter "2"?)

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[blue]@[/blue] Code Couch:
[blue]@[/blue] Twitter:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top