| | |
textarea count commas.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
my requirement is
textarea have 3 keywords. no need to count letters per line. but each keyword separated by commas. forexample: abc is one keyword, defgh is another keyword, kalpana is another keyword. so we are entering like this abc,defgh,kalpana into textarea . there is no need to count no.of characters per line. why b'coz we dont have length of the keywords. textarea must contain 3keywords seperated by commas. for this 3 keywords separated by only 2 commas. so we count only commas. i think u understood.
textarea have 3 keywords. no need to count letters per line. but each keyword separated by commas. forexample: abc is one keyword, defgh is another keyword, kalpana is another keyword. so we are entering like this abc,defgh,kalpana into textarea . there is no need to count no.of characters per line. why b'coz we dont have length of the keywords. textarea must contain 3keywords seperated by commas. for this 3 keywords separated by only 2 commas. so we count only commas. i think u understood.
0
#2 Nov 9th, 2009
I suggest that you 'explode' the string and then check that the parms exist in the array that is created.
0
#3 Nov 9th, 2009
Hey.
You could also use the substr_count function.
You could also use the substr_count function.
php Syntax (Toggle Plain Text)
if(substr_count($input, ',') == 2) { echo "Valid!;" }
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
-1
#4 Nov 9th, 2009
Fair enough, what happens if somebody (I assume you have users entering data, not just yourself) puts a comma at the end? The reason I ask is because I do this all the time without thinking. Do you get an error message or do you check to see if the fourth term is blank?
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
0
#5 Nov 10th, 2009
•
•
•
•
Fair enough, what happens if somebody (I assume you have users entering data, not just yourself) puts a comma at the end? The reason I ask is because I do this all the time without thinking. Do you get an error message or do you check to see if the fourth term is blank?
PHP Syntax (Toggle Plain Text)
<?php if($_POST) { $textarea =$_POST[a]; $textarea_array= explode(",",$textarea); $count=count($textarea_array); //echo $count; if($count!=3) { echo "enter three keywords only and eachone separate by commas."; } else { for($i=0;$i<count($textarea_array);$i++) { echo $textarea_array[$i].','; } } } ?> <html> <head> <body> <form method="post"> <textarea name="a">some text</textarea> <input type="submit" name="submit" value="sub"> </form> </body> </head> </html>
0
#6 Nov 10th, 2009
PHP Syntax (Toggle Plain Text)
<html> <head> <script language="javascript"> function commas() { var a=document.form.a.value; var m=','; var c=0; for (var i=0;i<a.length;i++) { if(m==a.substr(i,m.length)) { c++; } } if(c!=2) { alert("enter three keywords only"); } } </script> </head> <body> <form method="post" name="form" onSubmit="return commas();"> <textarea name="a""></textarea> <input type="submit" name="submit" value="sub"> </form> </body> </html>
0
#7 Nov 10th, 2009
•
•
•
•
Fair enough, what happens if somebody (I assume you have users entering data, not just yourself) puts a comma at the end?
php Syntax (Toggle Plain Text)
if(substr_count(trim($input, ','), ',') == 2) { echo "Valid!;" }
@muralikalpana
I'm glad you found a solution.
However, relying on JavaScript alone is not a good idea. What if the client has it turned off?
It's best to combine JavaScript and PHP. That way you can be sure things are working right no matter what.
Consider this alteration to your code:
php Syntax (Toggle Plain Text)
<?php $error = false; // Used later to print the error message if(isset($_POST['a'])) { if(substr_count(trim($_POST['a'], ','), ',') != 2) { $error = true; } else { // Process your keywords here } } ?> <!DOCTYPE html> <html> <head> <title>Comma thingie</title> <script type="text/javascript"> function commas() { var seperator = ','; var value = document.form.a.value; var count = 0; // Remove trailing commas from the textarea value while(value[value.length-1] == seperator) { value = value.substr(0, value.length - 1); } document.form.a.value = value; for (var i = 0; i < value.length; i++) { if(seperator == value.substr(i,seperator.length)) { count++; } } if(count != 2) { alert("Only 3 keywords are allowed."); return false; } else { return true; } } </script> </head> <body> <?php if($error) { echo "\t<h3 style='color: red;'>Only 3 keywords are allowed.</h3>"; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form" onsubmit="return commas();"> <textarea name="a"><?php echo htmlentities(@$_POST['a']); ?></textarea> <input type="submit" name="submit" value="sub"> </form> </body> </html>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
0
#8 Nov 10th, 2009
•
•
•
•
Good point.
php Syntax (Toggle Plain Text)
if(substr_count(trim($input, ','), ',') == 2) { echo "Valid!;" }
@muralikalpana
I'm glad you found a solution.
However, relying on JavaScript alone is not a good idea. What if the client has it turned off?
It's best to combine JavaScript and PHP. That way you can be sure things are working right no matter what.
Consider this alteration to your code:
php Syntax (Toggle Plain Text)
<?php $error = false; // Used later to print the error message if(isset($_POST['a'])) { if(substr_count(trim($_POST['a'], ','), ',') != 2) { $error = true; } else { // Process your keywords here } } ?> <!DOCTYPE html> <html> <head> <title>Comma thingie</title> <script type="text/javascript"> function commas() { var seperator = ','; var value = document.form.a.value; var count = 0; // Remove trailing commas from the textarea value while(value[value.length-1] == seperator) { value = value.substr(0, value.length - 1); } document.form.a.value = value; for (var i = 0; i < value.length; i++) { if(seperator == value.substr(i,seperator.length)) { count++; } } if(count != 2) { alert("Only 3 keywords are allowed."); return false; } else { return true; } } </script> </head> <body> <?php if($error) { echo "\t<h3 style='color: red;'>Only 3 keywords are allowed.</h3>"; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form" onsubmit="return commas();"> <textarea name="a"><?php echo htmlentities(@$_POST['a']); ?></textarea> <input type="submit" name="submit" value="sub"> </form> </body> </html>
Last edited by muralikalpana; Nov 10th, 2009 at 7:10 am.
0
#9 Nov 10th, 2009
PHP Syntax (Toggle Plain Text)
<!DOCTYPE html> <html> <head> <title>Comma thingie</title> <script type="text/javascript"> function commas() { var seperator = ','; var value = document.form.textarea1.value; var count = 0; var murali=window.document.form.hidden1.value; alert(murali); // Remove trailing commas from the textarea value while(value[value.length-1] == seperator) { value = value.substr(0, value.length - 1); } document.form.textarea1.value = value; for (var i = 0; i < value.length; i++) { if(seperator == value.substr(i,seperator.length)) { count++; } } if(count != 5) { alert("Only 6 keywords are allowed."); document.form.textarea1.focus(); return false; } else { return true; } } </script> <script language="javascript"> function chgtx2() { alert(window.document.getElementById('radioa').value); if(window.document.form.radio.value==9.95) { window.document.form.hidden1.value=3; } else { window.document.form.hidden1.value=6; } } </script> </head> <body> <?php if($error) { echo "\t<h3 style='color: red;'>Only 3 keywords are allowed.</h3>"; } ?> <form action=" " method="post" name="form" onSubmit="return commas();"> Add 10 key words $9.95 <input type="radio" name="radio" id="radioa" value="9.95" onClick="chgtx2();" /> <input type="hidden" name="hidden1" > (OR) Add 20 keywords $19.95 <input type="radio" name="radio" id="radioa" value="19.95" onClick="chgtx2();" /> <textarea name="textarea1" onChange="commas();"><?php echo htmlentities(@$_POST['a']); ?></textarea> <input type="submit" name="submit" value="sub"> </form> </body> </html>
Last edited by muralikalpana; Nov 10th, 2009 at 10:38 am.
0
#10 Nov 10th, 2009
PHP Syntax (Toggle Plain Text)
<!DOCTYPE html> <html> <head> <title>Comma thingie</title> <script type="text/javascript"> function commas() { var seperator = ','; var value = document.form.textarea1.value; var count = 0; var murali=window.document.form.hidden1.value; var mur=parseInt(murali)-1; alert(murali); // Remove trailing commas from the textarea value while(value[value.length-1] == seperator) { value = value.substr(0, value.length - 1); } document.form.textarea1.value = value; for (var i = 0; i < value.length; i++) { if(seperator == value.substr(i,seperator.length)) { count++; } } if(count != mur) { alert('Only'+ murali+ 'keywords are allowed.'); document.form.textarea1.focus(); return false; } else { return true; } } </script> <script language="javascript"> function chgtx2() { alert(window.document.getElementById('radioa').value); if(window.document.getElementById('radioa').value==9.95) { window.document.form.hidden1.value=10; } else { window.document.form.hidden1.value=20; } } </script> </head> <body> <form action=" " method="post" name="form" onSubmit="return commas();"> <input type="hidden" name="hidden1" > Add 10 key words $9.95 <input type="radio" name="radio" id="radioa" value="9.95" onClick="chgtx2();" /> (OR) Add 20 keywords $19.95 <input type="radio" name="radio" id="radioa" value="19.95" onClick="chgtx2();" /> <textarea name="textarea1" onChange="commas();"><?php echo htmlentities(@$_POST['a']); ?></textarea> <input type="submit" name="submit" value="sub"> </form> </body> </html>
Last edited by muralikalpana; Nov 10th, 2009 at 11:24 am.
![]() |
Similar Threads
- radiobutton with textarea (PHP)
- Some help (counting lines from an input file) (C++)
- SELECT into TEXTAREA (PHP)
- how to format the form textarea? (PHP)
- how to delete extra commas on csv file with php (PHP)
- Need Help with TextArea and Loop (Java)
- Count/Find in const char * (C++)
- Java Program Help (Java)
- Word and Letter Count (C)
Other Threads in the PHP Forum
- Previous Thread: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
- Next Thread: Fatal error: Class 'cite' not found
Views: 577 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery js limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube






