form filed checking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

form filed checking

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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
}
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Great I will give it ago.

Thanks
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

< is less than
> is greater than
>= greater than or equal to
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post 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.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

fields have a 4 character limit.

Thanks
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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
}
;)
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post by wyred »

Ah, I see. When he said 4 character limit, I assumed any value up to a maximum of 4.
Post Reply