IreneLing Posted October 8, 2011 Share Posted October 8, 2011 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. Quote Link to comment Share on other sites More sharing options...
IreneLing Posted October 9, 2011 Author Share Posted October 9, 2011 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 . Quote Link to comment Share on other sites More sharing options...
IreneLing Posted October 9, 2011 Author Share Posted October 9, 2011 It seems I found a simple way in php , so sorry for this post and thanks for reading . 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.