IreneLing Posted October 9, 2011 Share Posted October 9, 2011 Hi all . In my scripts , there is a textbox that allow user to enter multiple phone number separate with "," , if all valid it will echo "ok!" else will echo "error" . How if I want to know which value is in wrong format? such as I entered "0112255666,445221122200" , then it will echo " 445221122200 is not a valid phone number" . And how to echo the total phone number inserted to the textarea ? Thanks for every reply . <?php if (isset($_POST["Submit"])) { $arrLines = split(",",$_POST['cellphonenumber']); foreach($arrLines as $cells){ if(!preg_match('/^[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/', $cells)){ echo "error"; } else{ echo "ok!"; } } } ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" name="myform" method="post"> <textarea name="cellphonenumber" rows="20" cols="100"></textarea> <input type="submit" name="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/ Share on other sites More sharing options...
awjudd Posted October 9, 2011 Share Posted October 9, 2011 In your "error" section where you echo "error" also echo the $cells value? As for counting the total number of successful add an incrementor that adds 1 every time it was successful and then outside of your foreach loop echo that value. Does this make sense? ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/#findComment-1277451 Share on other sites More sharing options...
IreneLing Posted October 9, 2011 Author Share Posted October 9, 2011 Thanks for your reply juddster , ok I understand the total number of successful add now , really thank you . In this scripts , it will just simply echo "error" or "ok" at the bottom of textarea , just a very simple code so I not yet arrange where to display the error message properly . Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/#findComment-1277453 Share on other sites More sharing options...
awjudd Posted October 9, 2011 Share Posted October 9, 2011 I'm not sure what you mean for the error section. Did you want it to have a list like: error 123456 error 1234567 etc? Or something else? ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/#findComment-1277457 Share on other sites More sharing options...
IreneLing Posted October 9, 2011 Author Share Posted October 9, 2011 Thanks for your reply again . Let's say , I enter "0164455888,0147788555,2365412258,221145589" (my cell number must start with 0 ) , so now there are 2 numbers are invalid . then after I click Submit button it will echo : " 2365412258 , 221145589 is not valid number" or " 2365412258 is not valid number" " 221145589 is not valid number". Is it something related to trim?I'm not sure how to do this part... Thanks again . Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/#findComment-1277461 Share on other sites More sharing options...
awjudd Posted October 9, 2011 Share Posted October 9, 2011 Either you can build an array of all of the errors and output it like you have it the first way, or just echo it as you go to get the output like you have the second way. <?php if (isset($_POST["Submit"])) { $arrLines = split(",",$_POST['cellphonenumber']); $errors = array (); foreach($arrLines as $cells) { if(!preg_match('/^[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/', $cells)) { $errors [] = $cells; } } } if ( count ( $errors ) == 0 ) { echo 'All Successful'; } else { foreach ( $errors as $error ) { echo $error; } echo ' is not a valid number.'; } ?> <form action="" name="myform" method="post"> <textarea name="cellphonenumber" rows="20" cols="100"></textarea> <input type="submit" name="Submit" /> </form> ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/#findComment-1277462 Share on other sites More sharing options...
IreneLing Posted October 9, 2011 Author Share Posted October 9, 2011 Thank you so much juddster , it's works , I will learn from it . Really thanks and have a nice day . Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/#findComment-1277469 Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 Worth pointing out that split() has was deprecated in favour of preg_split() as of PHP 5.3.0. So might be worth changing this now rather than later and avoid headaches. You can find some info regarding what is changing [m=http://www.php.net/manual/en/reference.pcre.pattern.posix.php]here[/m] Quote Link to comment https://forums.phpfreaks.com/topic/248743-show-value-of-multiple-invalid-input-in-textarea/#findComment-1279112 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.