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

How do I verify pre-submit 1

Status
Not open for further replies.

lakeside12

Programmer
Apr 20, 2009
43
CA
Hello

I have a form and it has a submit button as follows

<a href="javascript:document.forms[0].submit()"> ... url >

Is there a way to do a test on 2 form fields when the consumer clicks or hovers the mouse over the button, without leaving the form to process all the information.

I want to test 2 fields, one called Zipcode the other FName and I just want to make sure zip code has 5 numbers in it and Name is longer than 1 character. I just want a popup OK box that tells them to fill in the fields.

I would appreciate help on this as I am a novice at javascript

many thanks
 
Hi

I would use an [tt]input[/tt] of [tt]type="submit"[/tt] to submit that [tt]form[/tt]. Links were invented for other reasons.

Anyway, to check the [tt]form[/tt] before submitting just set an [tt]onsubmit[/tt] event handler :
Code:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]"whatever"[/i][/green] [highlight][maroon]onsubmit[/maroon][teal]=[/teal][green][i]"return checkme(this)"[/i][/green][/highlight][b]>[/b]
[gray]<!-- ... -->[/gray]
[b]</form>[/b]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]checkme[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] err[teal]=[/teal][green][i]''[/i][/green]
  [b]if[/b] [teal](![/teal]what[teal].[/teal]Zipcode[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/^[0-9]{5}$/[/fuchsia][teal]))[/teal] err[teal]+=[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i] - zip code must contain 5 digits'[/i][/green]
  [b]if[/b] [teal](![/teal]what[teal].[/teal]FName[teal].[/teal]value[teal].[/teal]length[teal])[/teal] err[teal]+=[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i] - first name must be completed'[/i][/green]
  [b]if[/b] [teal]([/teal]err[teal])[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'Errors occured :'[/i][/green][teal]+[/teal]err[teal])[/teal]
  [b]return[/b] err[teal]==[/teal][green][i]''[/i][/green]
[teal]}[/teal]

Feherke.
 
Feherke, my compliments. I love your approach to data validation. This is what I am working on today.

I would like to ask a question about the match function.

How could I modify this regular expression to check that the input has "at least" five digits?

Thanks.

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Thank you ever so much for the info on this worked perfectly

YOU ARE AMAZING
THANK YOU!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top