depojones Posted April 29, 2010 Share Posted April 29, 2010 Hello, How do i validate an input box only when a text is entered and not when empty. For instance, am validating a telephone number input box to confirm if it contains only numbers, but when left empty, it still output error. what do I do in this regard? Quote Link to comment https://forums.phpfreaks.com/topic/200174-input-box-validation/ Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 Could you please post the pertinent part of the code? Quote Link to comment https://forums.phpfreaks.com/topic/200174-input-box-validation/#findComment-1050557 Share on other sites More sharing options...
depojones Posted April 29, 2010 Author Share Posted April 29, 2010 Here is the test code: <?php if(isset($_POST['submit'])){ if(!is_numeric($cell)) { echo 'Cell number is incorrect'; } else{ echo 'Cell number is fine'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test</title> </head> <body> <form action="" method="post"> cell number: <input name="cell" type="text" value="<?php echo $_POST['cell']; ?>" /> <br /> <input name="submit" type="submit" value="test" /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/200174-input-box-validation/#findComment-1050559 Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 1. You never closed the FORM tag. 2. What's $cell? That variable is undefined. Quote Link to comment https://forums.phpfreaks.com/topic/200174-input-box-validation/#findComment-1050562 Share on other sites More sharing options...
depojones Posted April 29, 2010 Author Share Posted April 29, 2010 Typo error. I have updated it still having same problem. I want it to only validate when I enter a text and not when empty <?php $cell=$_POST['cell']; if(isset($_POST['submit'])){ if(!is_numeric($cell)) { echo 'Cell number is incorrect'; } else{ echo 'Cell number is fine'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test</title> </head> <body> <form action="" method="post"> cell number: <input name="cell" type="text" value="<?php echo $_POST['cell']; ?>" /> <br /> <input name="submit" type="submit" value="test" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/200174-input-box-validation/#findComment-1050566 Share on other sites More sharing options...
KevinM1 Posted April 29, 2010 Share Posted April 29, 2010 if (isset($_POST['submit'])) { if (!empty($_POST['cell'])) { if (!is_numeric($_POST['cell'])) { echo "Cell number is incorrect."; } else { echo "Cell number is valid."; } } else { echo "Cell number cannot be left empty."; } } Quote Link to comment https://forums.phpfreaks.com/topic/200174-input-box-validation/#findComment-1050572 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.