Forum Moderators: coopster

Message Too Old, No Replies

Trying to validate using php and javascript

         

obeamma

2:04 pm on Dec 22, 2007 (gmt 0)

10+ Year Member



Hello

Here comes another weird question:

I am starting to understand PHP. I am trying to validate a form I created. I wanted to know how do you validate a form using both php and javascript? For example if I am trying to validate firstname of a person.

Thanks

PHP_Chimp

8:14 pm on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Form validation is a huge topic.
So I would suggest that you Google for ideas. As there are lots of different ways to validate - regular expressions are used quite often.

If you want a more integrated approach then use ajax to validate everything with php.

You could check that the first name contains only letters using the function


if ([url=http://uk3.php.net/manual/en/function.preg-match.php]preg_match[/url]('%[a-z]+%i', $fname)) {
// name is ok
}
else {
// not ok
}

<edit>
added the link to preg_match so you can check it out :)

[edited by: PHP_Chimp at 8:17 pm (utc) on Dec. 22, 2007]

eelixduppy

12:49 am on Dec 23, 2007 (gmt 0)



ajax is not needed for validation like this because you'll find that you are going to have to validate it again with php anyway so it's just a waste of javascript coding. You should at least have some sort of server-side scripting validation similar to how it is presented in the post above this.

As for the javascript validation it is really only there for the convenience of the user. javascript allows the user to know right away if there is a problem with their form input so that they don't have to submit the form first to find out; however some browsers may not have javascript enabled in which case it would not do any validation if you were to only have that client-side validation present.

obeamma

6:01 am on Dec 23, 2007 (gmt 0)

10+ Year Member



okay now I understand...thanks guys for the info

PHP_Chimp

11:35 am on Dec 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although I agree with eelixduppy in as far as ajax is redundant it does help as any server side technology doestn't give immediate feedback. If you use ajax then you can provide that immediate feedback.
An example you be people with double barreled surnames. A lot of sights wont accept - in a name field. So the person enters there surname Smith-Andrew (for example) they wait for a few seconds then they get a warning saying 'This field can only contain letters'. Using ajax they dont need to wait for validation of all fields and they can go back and remove the -. They then need to look through the (hopefully) reloaded form to find the incorrect field and amend it. Would it not be better if a second after they tab out of that field they get the warning? As they can immediately go back and amend.

Ajax is redundant, however it helps to keep your customers happy, as they get immediate feedback about the validity of there entries.
However NEVER just javascript validate, as people can turn that off, then you have no validation.

eelixduppy

6:13 pm on Dec 24, 2007 (gmt 0)



I agree that immediate feedback is a good idea, however ajax isn't necessary to give that feedback right away; it could simply be a solution written in javascript without having to send a request to the server for validation in the background. You could maybe use the onBlur or onKeyUp event handlers to check the contents of the field with javascript ONLY. A request doesn't have to be sent back to the server for validation; it could if you wanted to, but that would be unnecessary.

PHP_Chimp

8:57 pm on Dec 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im lazy...I cant be bothered to write 2 different validation routines, so use ajax so I only need to write 1 :)

But yes you are correct that you could write the same thing twice.