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 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validation functions

    Whate are good function(s) for form validation and could you show your function with an example....

    Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ummm? bit random? do you mean anything specific? there are literally thousands of different methods for form checking.

    well, here is a little function i just wrote for checking for invalid characters.

    PHP Code:

    function has_invalid_chars($str$chars){

          
    $return true;

          
    $invalid_chars preg_split("/(\s{0})/is"$chars);
          
    array_shift($invalid_chars);
          
    array_pop($invalid_chars);

          foreach(
    $invalid_chars as $invalid){

                
    $match '/'.preg_quote($invalid'/').'/is';
                if(
    preg_match($match$str)){

                      
    $return false;

                }

          }

          return 
    $return;

    }

    // you could use it like this:

    has_invalid_chars('lots of \\invalid *.chars,/()''\\.*,/()[]'); 
    EDIT: Just so you know, this is untested.

  • #3
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't need something like that

    I mean right now I am using
    PHP Code:
    if(trim($var1,$var2) == ""){
    echo 
    "You left something out!";

    Just wondering If there are any more to choose from....
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, if you ask a very general question, and someone gives a you a specific example, and you say don't need something like this, then you really need to clarify what you are looking for, eh? Doesn't make much sense if we start posting our validation routines and you respond to each of them with "nope, that's not what I want", IMO.

    But speaking about useful functions I'd use to validate forms these spring to mind: isset(), strlen(), intval(), and of course my favorite: preg_match(). Since all form content comes into your script as text strings (ok, some are arrays of strings), regular expression are an ideal tool to check if the value matches a specific format such as year numbers, date formats, urls, etc...
    The string functions part of the manual has a lot of more specific functions which may have their use too. Have look into the documentation examples and the user comments. Often the best place for starting to use a specific function.
    De gustibus non est disputandum.

  • #5
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you show me how preg_match() is used?
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure. Here's a *very* simple email check:

    Code:
    $email = 'someone@example.com';
    
    if (preg_match('/^([a-z\._-])+@([a-z\._-])+$/i', $email)) {
    print "valid email";
    }
    The key part is the regular expression used in the first argument to preg_match(). RegExps are a useful tool, but they take some time to get comfortable with them. Essentially, you define a pattern by a mini-language, and your string to test must conform to this pattern.
    De gustibus non est disputandum.

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    also (SDP), your trim() method, although handy for simple validation, does not provide any other form of checking.

    eg:

    enter a password ( \ / * : ) are invalid characters

    you will then need to make some separate code to handle this password, hence the code i have allows you to stop the useage of these characters. My example uses preg_match() but in a more dynamic form (eg: you dont see the actual regular expression.

    Im not saying you should use that method of validation, I only just wrote it when i read the thread, although i might use it now as it is a handy function. Its best to have some general ideas on form processing before you go ahead and write a script.

    (P.S. If you have any probs with regex, mordred is the one you want to ask )


  •  

    Posting Permissions

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