Results 1 to 4 of 4

Thread: [RESOLVED] Php input validation - exact input only

  1. #1
    Join Date
    Dec 2009

    resolved [RESOLVED] Php input validation - exact input only


    I am struggling in trying to find a solution for the above problem. I have a simple html form and have some php code set up to validate the user input. Below is an example of how i am validating one particular field:

    function validateField1($field1){
    		//if it's NOT valid
    		if(strlen($field1) < 1)
    			return false;
    		if(!preg_match('/^[A-Za-z0-9 ]+$/', $field1))
    		return false;
    		//if it's valid
                    return true;
    So for that field if the user fails to enter anything or if they include anything other than Lower/Uppercase letters, numbers and a space then the script returns false and brings up the error.

    Now i am trying to set up some fields where by the user must match their input with an exact string that i will set for it to correctly validate otherwise it will flag an error.

    So for example on 'field 2' - the user must enter the input of 'red' or 'blue' or 'green' or 'pink'. Anything other than either 1 of these will not be passed.

    Now i know instantly from that it would seem i should just use the 'select name' and 'option' values for my html to just have those 4 options in a drop down list to select but it needs to be a typed user input not selected from a list.

    Am i able to add exact terms into the above validation example or do i need to use a different technique? I have a feeling that it might be best to use MySql with this in the form of a database but i unfortunately have no knowledge of this language at present...

    Is anyone able to help me out on this one?

    Many thanks


  2. #2
    Join Date
    Apr 2003
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Feb 2009
    First of all, even dropdown lists need to be validated, because in the end, the user does not need to use the actual form in order to send GET or POST information to the script, so ALL fields should be validated (even checkboxes).

    Here is my method for form validation, starting with the one you need:

    PHP Code:
    //Only red, green, or blue are ok
    if(strtolower($_POST['input1']) == "red" || strtolower($_POST['input1']) == "blue" || strtolower($_POST['input1']) == "green")
    $input1 $_POST['input1'];
    //Error handler here

    //Only letter and numbers allowed
    if( !preg_match("/[^a-zA-Z0-9]/"$_POST['input2']) ) //the ^ inside of brackets is a NOT operator, so if there are any characters other than letters or numbers, the preg_match will return true
       //No error

    "Build a man a fire and he'll be warm for a day. Set a man on fire and he'll be warm for the rest of his life."

  4. #4
    Join Date
    Dec 2009
    Thanks Stevish, so simple really, got it all sorted now...

    Thank you again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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