Jump to content

Show value of multiple invalid input in textarea


IreneLing

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 .

Link to comment
Share on other sites

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 .

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.