fallenangel1983 Posted March 4, 2008 Share Posted March 4, 2008 My question is a simple one but i am having a hard time solving it and wondered if anyone could help. I have a html page with a form on it linking directly to a php page in which i want to do some validation. i have five text boxes in which a user may enter data. these are 1. desirable feature, 2. firstname, 3. last name, 4. telephone number and 5. email address. this is the code i have for the validation: ##### function checkDesFea($checkDesFea) { if(!preg_match("/[^a-zA-Z]+$/ ",$checkDesFea)) return TRUE; else return FALSE; } if(!checkDesFea($Feature)) { $error++; echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Feature-----</H3></Font>"; } echo "<h3> Desirable Feature: </h3> $Feature"; echo "<br><h3> Contact Details: </h3>"; ##### function checkFName($checkFName) { if(!preg_match("/[^a-zA-Z]+$/ ",$checkFName)) return TRUE; else return FALSE; } if(!checkFName($FName)) { $error++; echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid First Name-----</H3></Font>"; } ##### function checkLName($checkLName) { if(!preg_match("/[^a-zA-Z]+$/ ",$checkLName)&&($Feauture=='')) return TRUE; else return FALSE; } if(!checkLName($LName)) { $error++; echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Last Name-----</H3></Font>"; } ##### function checkTeNum($checkTeNum) { if(!preg_match("/[0-9]{11}$/",$checkTeNum)) return TRUE; else return FALSE; } if(!checkTeNum($TeNum)) { $error++; echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Telephone Number-----</H3></Font>"; } ##### function checkEmail($checkEmail) { if(!preg_match("/^([a-z0-9._-](\+[a-z0-9])*)+@[a-z0-9.-]+\.[a-z]{2,6}$/i",$checkEmail)) return TRUE; else return FALSE; } if(!checkEmail($Email)) { $error++; echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Email-----</H3></Font>"; } echo "<p><br> First Name: $FName"; echo "<br> Last Name: $LName"; echo "<br> Phone Number: $TeNum"; echo "<br> Email address: $Email"; The code i have seems to work ok but it allows the user to leave the textfield blank. I do not want to allow thee user to leave a textfield blank. how can i get around this issue. i have tried an embedded if statement and a while loop but to no avail. Any suggestions would be appreciated or if you can think of an easier way to validate the dat let me know as the Preg_Match function seems confusing to me lol thanks again FallenAngel Quote Link to comment Share on other sites More sharing options...
daydreamer Posted March 4, 2008 Share Posted March 4, 2008 Hello. I followed a tutorial the other day, and it uses this code to check if a field is blank: <?php if(!$_POST['first_name']){ // $_POST['first_name'] is the first_name field from your html form. $errors .= "Missing First Name\n"; // loads more code here ?> Anyway theres bound to be loads of tutorials on google for php validation check some out! Quote Link to comment Share on other sites More sharing options...
dbo Posted March 4, 2008 Share Posted March 4, 2008 if( isset($_POST['textfieldhere']) && $_POST['textfieldhere'] != "" ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.