By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

help with form field validation

P: n/a
hello,

i am trying to validate a form field ... requiring the user to enter 1
of 2 possible security codes (for example 1000 or 9975).

this part of my code does not seem to working - it gives the error
message when I enter the correct security code(s).

if ($_POST['security_question'] != 1000 || 9975) {
$errors[]= 'You have entered an incorrect security code.';
}

any tips on handle this? i am somewhat new to php - any info is much
appreciated. thanks

May 29 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
At Mon, 28 May 2007 18:22:11 -0700, hmlarson let h(is|er) monkeys type:
hello,

i am trying to validate a form field ... requiring the user to enter 1
of 2 possible security codes (for example 1000 or 9975).

this part of my code does not seem to working - it gives the error
message when I enter the correct security code(s).

if ($_POST['security_question'] != 1000 || 9975) {
$errors[]= 'You have entered an incorrect security code.';
}

any tips on handle this? i am somewhat new to php - any info is much
appreciated. thanks
Your code :

if ($_POST['security_question'] != 1000 || 9975)

should be something like:

if (($_POST['security_question'] != 1000) &&
($_POST['security_question'] != 1000)) {
[...]
}

It's easy to see this would become a lengthy script once there are more
valid answers. Imagine there are 10 valid codes, or 50...

So another way of doing it would be easier to maintain:

$valid_codes = array (1000,1248,1722,1769,9975,9999);
if (! in_array ($_POST['security_question'], $valid_codes)) {
// do 'invalid code stuff
}

Now adapting the script to accept other values as well is easy, just add
them to the $valid_codes array.

There are other ways still, as is often the case in programming. Ask 10
people and you might get 5 different solutions.

Good luck (with the homework ???? yes ???).
Rgds,

--
Schraalhans Keukenmeester - sc*********@the.spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') is -1"

May 29 '07 #2

P: n/a
At Tue, 29 May 2007 07:27:06 +0200, Schraalhans Keukenmeester let h(is|er)
monkeys type:
At Mon, 28 May 2007 18:22:11 -0700, hmlarson let h(is|er) monkeys type:
>hello,

i am trying to validate a form field ... requiring the user to enter 1
of 2 possible security codes (for example 1000 or 9975).

this part of my code does not seem to working - it gives the error
message when I enter the correct security code(s).

if ($_POST['security_question'] != 1000 || 9975) {
$errors[]= 'You have entered an incorrect security code.';
}

any tips on handle this? i am somewhat new to php - any info is much
appreciated. thanks

Your code :

if ($_POST['security_question'] != 1000 || 9975)

should be something like:

if (($_POST['security_question'] != 1000) &&
($_POST['security_question'] != 1000)) {
[...]
}
Oops, typo.

if (($_POST['security_question'] != 1000) &&
($_POST['security_question'] != 9975)) {
[...]
}

Sorry.

Sh.
May 29 '07 #3

P: n/a
On May 29, 6:22 am, hmlarson <hlar...@gmail.comwrote:
hello,

i am trying to validate a form field ... requiring the user to enter 1
of 2 possible security codes (for example 1000 or 9975).

this part of my code does not seem to working - it gives the error
message when I enter the correct security code(s).

if ($_POST['security_question'] != 1000 || 9975) {
$errors[]= 'You have entered an incorrect security code.';

}

any tips on handle this? i am somewhat new to php - any info is much
appreciated. thanks
u can also use mysql db for validating security codes if u hv many. if
u want its code, then tell me.

May 29 '07 #4

P: n/a
Thanks!!!

May 29 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.