Newbie help - form validation of a number

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
farley2k
Forum Newbie
Posts: 5
Joined: Thu Jan 15, 2009 1:30 pm

Newbie help - form validation of a number

Post by farley2k »

I need to create a really simple page where we validate a number entered by a user. They enter their library card number if it is 13 characters long and starts with "10001" then they get directed to a web page, if not they get dumped back to the data entry page with a message that they need to enter a valid library card number. We don't need to check that it is actually a real library card number - just that it is 13 characters and starts with "10001" So this seems pretty simple, but I am very, very new to PHP and I would love some help.

I think I need to use the strlen function to check that it is 13 characters so something like "$libIDLength = strlen($libID)" Then I could say if $libIDLength == 13 redirect, if not error.

I am not sure how to get the first 5 characters and check to see if they are "10001" though.

I am also not sure how to do a two condition If statement. The data needs to be both 13 character and start with "10001" but all the If/else examples I have seen just have one condition for each else.

Can someone help me sort this out a bit?

Thank you so much for any help
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Newbie help - form validation of a number

Post by deejay »

Using regu;ar expression would be the proper way to do it, I would have thought. http://uk2.php.net/manual/en/book.regex.php pretty frightning for a newb though.

Otherwise you could use something along the lines of

Code: Select all

 
if ( $condition1 && $condition2){
 
}
 
condition 1 you could use substr and then look for an exact match - http://uk2.php.net/manual/en/function.substr.php

and condition 2 you could use http://uk2.php.net/manual/en/function.strlen.phpas you mentioned.

Hope that helps.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie help - form validation of a number

Post by requinix »

farley2k wrote:I am not sure how to get the first 5 characters and check to see if they are "10001" though.
There are two simple ways to do this. There are undoubtedly more, of course, but...

1. strncmp. It compares the first X letters of two strings. You can use it to compare the number and "10001" but using only the first 5 characters.
2. substr. It grabs a sequence of letters from a string based on a starting position and a length. Get the first (start=0) five (length=5) characters of the library card number and compare it normally (using ==) with "10001".

You can pick whichever makes more sense to you.
farley2k wrote:I am also not sure how to do a two condition If statement. The data needs to be both 13 character and start with "10001" but all the If/else examples I have seen just have one condition for each else.
There's also an "elseif" part. Take a look here, there's an example of how to use it.

As with most things in PHP there are two ways you can check for a valid number. You can check if the string isn't the right length (then you show a message); if it passes that test you check if it doesn't start with 10001 (you show a message). If it passes both those tests then you continue as normal.

Code: Select all

if ($failed_first_test) {
    show_message();
} else if ($failed_second_test) {
    show_message();
} else {
    is_good();
}
Or you can check that it's the right length and starts with 10001 at the same time with a logical AND: &&.

Code: Select all

if ($passed_first_test && $passed_second_test) {
    is_good();
} else {
    show_message();
}

About using a regex: there's no need. They're slower than other string functions (like strncmp and substr) because they give you a lot of power. If you don't need that power then it's a bit much for so little.
farley2k
Forum Newbie
Posts: 5
Joined: Thu Jan 15, 2009 1:30 pm

Re: Newbie help - form validation of a number

Post by farley2k »

Thanks I will be reading up on all these functions to figure our how to toss them into my code.
Post Reply