Jump to content

Validation for textarea


IreneLing

Recommended Posts

Hi all .

In my script , there is a textarea that allow user to enter multiple data and insert into mysql.

 

Is there any way to validate the data typed in the textarea is in correct format?

 

Let's say user can only type different cellphone number:

 

|------------------------------------------------------|

|1120000114;0026644558;1123366987;  |

|                                                                  |

|                                                                  |

|                                                                  |

|_________________________________ |

 

How can I validate that each number is in correct format? and show the total number of recipients?

 

Here is my code:

<?php

if (isset($_POST["Submit"])) {


$_POST['names'] = preg_replace('~[\s]{2,}~', ' ', $_POST['names']); 
$names = mysql_real_escape_string($_POST['names']);
$names_exp = preg_split("([, ;\n])", $names);
$query = "INSERT INTO esp VALUES "; 
$records = array(); 

foreach($names_exp as $value) {



$records[] = "(null, '$value', '{$_POST['type']}')"; 
}

$query .= implode( ', ' , $records ); 

if( $result = mysql_query($query) ) {



echo "Success! You have successfully sent in " . mysql_affected_rows() . " names! ";
}

}
?>
<html>
<body>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
  <textarea cols="50"  rows="20" name="names"></textarea><br />
  <input type="submit" value="Submit" name="Submit"/>
</form>

</body>
</html>

 

Thanks for every reply.

Link to comment
Share on other sites

Ok gotta change my question here , now I have some code that is working on validate phone number . And another code is store phone numbers into mysql . I want to make them work together , such as if there is invalid data in textarea , a pop-up alert message will appear . If not , then it will redirect to insert my data . I tried many methods but still not working....any advices or hints can I get here?

 

For phone validation part:

function validate(cell) {
   var reg = /^[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/;

   return reg.test(cell);
}

function trim(str){
var	str = str.replace(/^\s\s*/,''),
	ws = /\s/,
	i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}

function checkAll(){
var status = true;
var i = 0;
var cells = document.myform.cellphonenumber.value.split("\n");
for(i=0; i<cells.length; i++){
	if(!validate(trim(cells[i]))){			
		alert("Incorrect format: "+cells[i]);
		status = false;
		break;
	}
}
if(status){
	alert("List validated, "+i+" cells found");
                submitcell();
}
}

<input type="button" onclick="checkAll();" value="check" />

 

But for insert into mysql part:

<?php

if (isset($_POST["Submit"])) {


$_POST['names'] = preg_replace('~[\s]{2,}~', ' ', $_POST['names']); 
$names = mysql_real_escape_string($_POST['names']);
$names_exp = preg_split("([, ;\n])", $names);
$query = "INSERT INTO esp VALUES "; 
$records = array(); 

foreach($names_exp as $value) {



$records[] = "(null, '$value', '{$_POST['type']}')"; 
}

$query .= implode( ', ' , $records ); 

if( $result = mysql_query($query) ) {



echo "Success! You have successfully sent in " . mysql_affected_rows() . " names! ";
}

}
?>

<input type="submit" value="Submit" name="Submit"/>

 

Thanks for every reply .

 

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.