Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-12-2010, 06:21 PM   PM User | #1
banjax
New Coder

 
Join Date: Jun 2009
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
banjax is an unknown quantity at this point
Smile Validating existing JavaScript code (for more than checking entry). Email etc.

Hi, I'm wondering if someone can help me. I have recently used this tutorial:

http://net.tutsplus.com/tutorials/ph...-confirmation/

to develop a more complex sign up form for a competition for a client. Basically, the way the validation has been done is different to many other validation methods I've used before and don't understand how to implement it, for some additional requirements.

This is an example of the PHP validation code that I have altered, to suit my form requirements:

Code:
//quick/simple validation
 if(empty($code)){ $action['result'] = 'error'; array_push($text,'You forgot to enter your entry code'); }
 if(empty($name)){ $action['result'] = 'error'; array_push($text,'You forgot to enter your name'); }
 if(empty($email)){ $action['result'] = 'error'; array_push($text,'You forgot to enter your email address'); }
This code only validates that there is content in those fields. But, for example on the 'entry code' field, I would like to put a maximum character input there and restrict certain characters, as well as a standard 'email' validation, so that the form is sent through to the database correctly.

If anyone knows how I can do this, with this sign-up form in particular, please assist me, as every other method I've tried to work with this form hasn't worked and caused the form to error.

Thank you in advance.

Last edited by banjax; 04-13-2010 at 12:43 PM..
banjax is offline   Reply With Quote
Old 04-12-2010, 06:31 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
er, that’s PHP code, but this is the JavaScript forum.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 04-12-2010, 06:43 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,032
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Form validation using JavaScript has been covered a zillion times in this forum.

Validation of the form if(empty($name)){ is barely worthy of the name, and virtually useless, as even a single space, an x or a ? will return false, that is pass the validation.

Example of validation for email:-

Code:
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email.value))) {   // returns true if invalid


Quizmaster: Name the 4th state of the USA to join the Union. It was named after a British king.
Contestant: Texas.

Last edited by Philip M; 04-12-2010 at 07:10 PM..
Philip M is offline   Reply With Quote
Old 04-13-2010, 12:48 PM   PM User | #4
banjax
New Coder

 
Join Date: Jun 2009
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
banjax is an unknown quantity at this point
Smile

Quote:
Originally Posted by Philip M View Post

Code:
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email.value))) {   // returns true if invalid
Philip,

I have tried to integrate your code example into my PHP's Validation (as above) like this:

Code:
	if(empty($code)){ $action['result'] = 'error'; array_push($text,'You forgot to enter your entry code'); }
	if(empty($name)){ $action['result'] = 'error'; array_push($text,'You forgot to enter your name'); }
	if(!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test($email.value))) {$action['result'] = 'error'; array_push($text,'Please enter a valid email address'); }
When I refresh the page, all data seems to clear and I can't see anything, never mind check the validation.

Have I written everything correctly?

Thanks in advance and sorry for my stupidity, I'm new to PHP and I just can't make sense of this.
banjax is offline   Reply With Quote
Old 04-13-2010, 12:57 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,032
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The code I offered you is JavaScript, not PHP. You posted in the JavaScript forum so I understood that was what you wanted. You will of course have to validate server-side as well.

Last edited by Philip M; 04-13-2010 at 01:01 PM..
Philip M is offline   Reply With Quote
Old 04-13-2010, 01:42 PM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Make up you mind: you have a PHP server-side validation problem or a JavaScript client-side validation problem?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Tags
database, form-validation, forms, javascript, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:12 PM.


Advertisement
Log in to turn off these ads.