Forum Moderators: coopster

Message Too Old, No Replies

Can anyone suggest a script?

         

glamdring

6:25 pm on Jun 15, 2004 (gmt 0)

10+ Year Member



Hi all.

Can anyone suggest a script which I can get hold of to validate :

several text boxes, a couple of drop-downs and a checkbox, before allowing me to enter my data into mySQL.

I've been hunting high and low for one and can't find a decent one anywhere. I dont know how to write my own - I wish I did.

Thanks in advance.

dcrombie

6:44 pm on Jun 15, 2004 (gmt 0)



Validate what? That text has been entered and some checkboxes checked? Or something more challenging?

What have you got so far?

glamdring

7:03 pm on Jun 15, 2004 (gmt 0)

10+ Year Member



Yep - just want to validate that some textfields have been completed (contain name, number, email etc) and that one checkbox has been ticked.

All I've got is the form.

I dont know where to start to be honest.

The data needs to go into SQL db afterwards, which I can get it doing at the moment.

Any help or just some suggestion as to how to get started would be great.

glamdring

1:10 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



Anyone? LOL

If someone could point me in the right direction i'd be more than happy.

If there's an alternative method of validation other than PHP I'd be happy to hear about it. I'd rather stay away from JScript but I'm open to suggestions.

Thanks.

coopster

1:11 am on Jun 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'll often have a javascript validation method but always, always follow up with server-side validation. That gets written first and I'll adapt the js afterward.

Show us an example of what you are after and we'll get you started on the server-side validation...

ergophobe

11:35 pm on Jun 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



for starters....

session_start();

$field1_min_length = 6;
$_SESSION['error_msg'] = "";
$_SESSION['bad_fields'] = array();

if (isset($_POST['field1']) && (strlen($_POST['field1']) > $field1_min_length))
{
$set_clause .= "field1='" . $_POST['field1'] . "',";
}
else
{
$_SESSION['error_msg'] .= "Problem with Field 1";
$_SESSION['bad_fields']['field1'] = true;

}

.... etc

if ($_SESSION['error_msg'])
{
show form again with error message and bad fields highlighted.
}
else
{
show the confirmation page.
}

Spontan

1:36 am on Jun 21, 2004 (gmt 0)

10+ Year Member



using php it looks like using the eregi() function may be best here. Thats what i used to make sure they were filled in at least 4 chars and non-blank.

you can check eregi() out at php.net and they'll even have an example how to use

As for the checkboxes..if they dont' check any then the array say..if u use 1 array i assume won't even exist so you can just run an if statement on it..

This seems easy enough.

ergophobe

4:28 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Thats what i used to make sure they were filled in at least 4 chars and non-blank.

Unless you want to check for specific patterns (like that it is all alpha-numeric, underscores or hyphens), you should stick with strlen() since it will be more efficient.

For many fields, you *will* want to check more than just string length, for others you can't if there is great variation in terms of what will be entered.

Tom