Monday, April 30, 2012

preg_matchvalidation

Okay, everything I've checked on this site referring to validation isn't what I'm looking for.



What I'm looking to do is a minimum length and maximum length of a value in firstname and secondname, this is the code which I currently have.



        if (isset($_POST['submit'])) {
$errors = array();

if (isset($_POST['firstname'])) {
$fn = $_POST['firstname'];
} else {
$errors[] = "You have not entered a first name";
}

if (isset($_POST['secondname'])) {
$sn = $_POST['secondname'];
} else {
$errors[] = "You have not entered a second name";
}


I was just wondering how would I apply preg_match to those which the minimum is 4 letters and the maximum is 15?



I do know it's something to do with



if(preg_match('/^[A-Z \'.-]{4,15}$/i', $_POST['firstname']))


In doing this I tried to do



    if (isset($_POST['firstname']) && preg_match('/^[A-Z \'.-]{4,15}$/i', $_POST['firstname')) {


But that also gave me an error :/



Could anyone give me a solution for this?



Thanks!



UPDATE:-



Nvm, I found a way around it. I just did this



if (isset($_POST['firstname'])) {
if (preg_match('/^[A-Z \'.-]{4,15}$/i', $_POST['firstname'])) {
$fn = $_POST['firstname'];
} else {
$errors[] = "<center> <h3> You must enter between 4 and 15 characters! </h3></center>";
}
} else {
$errors[] = "You have not entered a name";


}
For both the firstname and secondname. :)





No comments:

Post a Comment