Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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 01:43 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,446
    Thanks
    13
    Thanked 361 Times in 357 Posts
    er, that’s PHP code, but this is the JavaScript forum.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    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 08:10 PM.

  • #4
    New Coder
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    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 02:01 PM.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Make up you mind: you have a PHP server-side validation problem or a JavaScript client-side validation problem?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •