SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Form Validation

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Validation

    Hi, using PHP and CSS, how do I change the text colour of a label and border colour of a input field upon a user not entering data into any of the fields, or entering an invalid email address or phone number (must be 11 characters)?

    Code:
    <body>
    <form name="form1" method="post" action="">
      <label>Name:</label>
      <input name="name" type="text" id="name">
      <br>
      <br>
      <label>Email:</label>
      <input name="email" type="text" id="email">
      <br>
      <br>
      <label>Phone:</label>
      <input name="phone" type="text" id="phone">
      <br>
      <br>
    <label>Gender:</label>
      <select name="select">
        <option>Male</option>
        <option>Female</option>
      </select>
      <br>
      <br>
      <input type="submit" name="Submit" value="Submit">
    </form>
    </body>

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here and here
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Member
    Join Date
    May 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use ajax

  4. #4
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xiongbin View Post
    use ajax
    ajax has no place in this post. stop throwing out fancy words.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2008
    Location
    London, UK
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Form validation is probably one of the few things where javascript works really well. I don't think there is any need for ajax for form validation.

  6. #6
    SitePoint Member webxstudio's Avatar
    Join Date
    Feb 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by kronikmedia View Post
    Form validation is probably one of the few things where javascript works really well. I don't think there is any need for ajax for form validation.
    Client side validation is too easy to circumvent. Server side validation is always better. Just my 2 cents. I would use "Regular Expressions" to validate HTML form input.

    Search "Regular Expression" or "Regex" in your favorite search engine and enjoy the power of Regular Expressions.

    Last edited by webxstudio; Jun 14, 2008 at 14:21. Reason: Type-O

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your HTML code set do something like this;

    PHP Code:
    <label <?php if($errorGender){print 'class="error"';} ?>Gender:</label>
    Now when you process the form if there's an error you set $errorGender to TRUE and when the page reloads it will add the class "error" to the label. You can define the error class in your CSS however you like.

    That's a quick way of doing it. You can do something like $errorEmail, $errorName and so on and add the above example code to each label.


Bookmarks

Posting Permissions

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