| | |
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 23 Days Ago
I suggest that you 'explode' the string and then check that the parms exist in the array that is created.
0
#3 23 Days Ago
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 23 Days Ago
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?
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
0
#5 23 Days Ago
•
•
•
•
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 23 Days Ago
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 23 Days Ago
•
•
•
•
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 23 Days Ago
•
•
•
•
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; 23 Days Ago at 7:10 am.
0
#9 22 Days Ago
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; 22 Days Ago at 10:38 am.
0
#10 22 Days Ago
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; 22 Days Ago 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
| Thread Tools | Search this Thread |
apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl data database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip javascript joomla limit link links login mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion remote script search searchbox server session sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validator variable video virus votedown web website window.onbeforeunload=closeme; xml youtube





