Page 1 of 1

form filed checking

Posted: Sun Jun 19, 2005 7:41 pm
by Jim_Bo
Hi,

I am checking against 3 text fields:

Code: Select all

if ((!$fn1) || (!$fn2) || (!$fn3)) {
I have a 4 character limit on each field, what I need to do is make sure each field contains 4 characters before it can be successfully submited?

At the moment its only checking that each contains data.


Thanks

Posted: Sun Jun 19, 2005 7:48 pm
by nigma
Use php's strlen() function:

Code: Select all

if (strlen($field1) >= 4 && strlen($field2) >= 4 ...) {
   // fields filled with 4+ characters
} else {
   // fields not filled with 4+ characters of data
}

Posted: Sun Jun 19, 2005 7:50 pm
by Jim_Bo
Hi,

Great I will give it ago.

Thanks

Posted: Sun Jun 19, 2005 8:18 pm
by Jim_Bo
Hi

The following works on one field:

Code: Select all

if (strlen($fn1) < 4) {
But I cant get it add more to make it work on all 3 fields:

Code: Select all

if ((strlen($fn1) < 4) && (strlen($fn2) < 4)) {
Only seems to check the first field then submits the form even tho second field has only 2 characters.

Thanks

Posted: Sun Jun 19, 2005 8:19 pm
by John Cartwright
< is less than
> is greater than
>= greater than or equal to

Posted: Sun Jun 19, 2005 11:30 pm
by Jim_Bo
Hi,

Yer I know that much, I want each text field to have no less than 4 characters, so the code says < 4.

But its only stoping if the first field has less than 4 characters, If $fn2 has no data it should stop as there are less than 4 characters in that field etc..

Thanks

Posted: Mon Jun 20, 2005 12:16 am
by nigma
Jim_Bo wrote:

Code: Select all

if ((strlen($fn1) < 4) && (strlen($fn2) < 4))
This will work, and therefore the problem exists somewhere else. Maybe in the value of the $fn variables or what you think the values of those variables are, but not in the quoted code.

Posted: Mon Jun 20, 2005 12:47 am
by Jim_Bo
Hi,

Na it doesnt seem to work the variable is fine.

but if is use || (or) it works fine:

Code: Select all

if ((strlen($fn1) < 4) || (strlen($fn2) < 4)) {

Thanks

Posted: Mon Jun 20, 2005 1:55 am
by wyred
Jim_Bo, that won't work. As long as either variable has less than 4 characters, the code will be executed even if the other variable has more than 4 characters.

Posted: Mon Jun 20, 2005 2:48 am
by Jim_Bo
Hi,

fields have a 4 character limit.

Thanks

Posted: Mon Jun 20, 2005 1:24 pm
by Skara
wyred wrote:Jim_Bo, that won't work. As long as either variable has less than 4 characters, the code will be executed even if the other variable has more than 4 characters.
He wants it to fail if one is less than 4:

Code: Select all

if ((strlen($fn1) < 4) || (strlen($fn2) < 4)) {
  // uhoh, you need more characters
}
else {
  // ok, continue with script
}
;)

Posted: Mon Jun 20, 2005 10:22 pm
by wyred
Ah, I see. When he said 4 character limit, I assumed any value up to a maximum of 4.