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
Newbie help - form validation of a number
Moderator: General Moderators
Re: Newbie help - form validation of a number
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
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.
Otherwise you could use something along the lines of
Code: Select all
if ( $condition1 && $condition2){
}
and condition 2 you could use http://uk2.php.net/manual/en/function.strlen.phpas you mentioned.
Hope that helps.
Re: Newbie help - form validation of a number
There are two simple ways to do this. There are undoubtedly more, of course, but...farley2k wrote:I am not sure how to get the first 5 characters and check to see if they are "10001" though.
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.
There's also an "elseif" part. Take a look here, there's an example of how to use it.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.
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();
}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.
Re: Newbie help - form validation of a number
Thanks I will be reading up on all these functions to figure our how to toss them into my code.