Jump to content

php form validation.


atrum

Recommended Posts

Does any one know a way to validate that all fields are filled out in a form with out writing an if else statement for each field?

 

Here is kinda what I would like.

 

On my form, I have 10 fields.

I want to have the form validate that all fields are not equal to nothing before it submits the data.

 

I think I may have to put the fields into an array, but I am not to savy on php  yet.

 

 

any help any one can provide will be appreciated.

 

 

Link to comment
Share on other sites

would think that it might be better to use JS to do this then PHP. I guess you could loop through

all the form fields and see if they are empty and show an error or redirect of something.


foreach ($_POST as $field=>$value) {//Start foreach loop
   
	if(empty($value)) {
		//The field has no value so do something here
                       header('Location: http://www.example.com/');
	}
}// end of foreach loop

Or something like that, you could even pass the field that was empty back to the page through the URL in the $_GET[]. Hope this helps in some way

 

 

 

 

Link to comment
Share on other sites

You should use both javascript and php... javascript is client side only and will not validate your data if javascript support is disabled in the browser.

 

Check with javascript before the form is allowed to submit, then check with php by looping through the $_POST array.

Link to comment
Share on other sites

Thank you for the help.

 

Yeah the reason I don't want to use much java is because of the very reason that I don't want to risk the java validation not working, and thus alowing people to put just anything in the database.

 

I also need to setup someway to enforce a password requirements system.

For example.

 

Password must contain 1 upper case, 1 lower, 1 number, and 8 characters.

 

Does any one have any clue on things like that

Link to comment
Share on other sites

Hi

 

Javascript is useful to save some load on your server and save the user having to wait for the server to validate the form when there are obvious errors. But it is pretty much useless to prevent anyone maliciously entering dodgy data. Once it is into php you may as well fully validate it, checking each individual field.

 

If you are feeling a bit masochistic then you can use Ajax to validate the form fields as they are entered. With a bit of care you can include the same php to validate the fields both via Ajax and via your main script. Can work well.

 

All the best

 

Keith

Link to comment
Share on other sites

You could prefix the names of form data with spmething specific and universal and run a loop...

 

foreach ($_POST as $key => $val) {
  if (substr($key,0,4)=='frm_') {
    switch ($key) {
      case 'frm_name':
        $strName=(strlen($_POST['frm_name'])>0 ? $_POST['frm_name'] : false);
        break;
      case 'frm_age':
        $intAge=(intval($_POST['frm_age'])>0 ? intval($_POST['frm_age']) : false);
        break;
    }
  }
}

 

That way you can check each variable at the end. If $intAge or $strName is false the data was invalid.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.