textarea count commas.

Thread Solved

Join Date: Sep 2009
Posts: 111
Reputation: muralikalpana is an unknown quantity at this point 
Solved Threads: 6
muralikalpana's Avatar
muralikalpana muralikalpana is online now Online
Junior Poster

textarea count commas.

 
0
  #1
23 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 213
Reputation: chrishea is an unknown quantity at this point 
Solved Threads: 32
chrishea's Avatar
chrishea chrishea is offline Offline
Posting Whiz in Training
 
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.
Chris
See my list of PHP development tools at:
InnovationsDesign.net
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #3
23 Days Ago
Hey.

You could also use the substr_count function.
  1. if(substr_count($input, ',') == 2) {
  2. echo "Valid!;"
  3. }
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 986
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 128
ardav's Avatar
ardav ardav is offline Offline
Posting Shark
 
-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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 111
Reputation: muralikalpana is an unknown quantity at this point 
Solved Threads: 6
muralikalpana's Avatar
muralikalpana muralikalpana is online now Online
Junior Poster
 
0
  #5
23 Days Ago
Originally Posted by ardav View Post
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?
i tried like this. exactly i got an error like you said.
  1. <?php
  2. if($_POST)
  3. {
  4. $textarea =$_POST[a];
  5. $textarea_array= explode(",",$textarea);
  6. $count=count($textarea_array);
  7. //echo $count;
  8. if($count!=3)
  9. {
  10. echo "enter three keywords only and eachone separate by commas.";
  11. }
  12. else
  13. {
  14.  
  15. for($i=0;$i<count($textarea_array);$i++) {
  16.  
  17. echo $textarea_array[$i].',';
  18.  
  19. }
  20. }
  21. }
  22. ?>
  23. <html>
  24. <head>
  25. <body>
  26. <form method="post">
  27. <textarea name="a">some text</textarea>
  28. <input type="submit" name="submit" value="sub">
  29. </form>
  30. </body>
  31. </head>
  32. </html>
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 111
Reputation: muralikalpana is an unknown quantity at this point 
Solved Threads: 6
muralikalpana's Avatar
muralikalpana muralikalpana is online now Online
Junior Poster
 
0
  #6
23 Days Ago
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. function commas()
  5. {
  6.  
  7. var a=document.form.a.value;
  8.  
  9. var m=',';
  10. var c=0;
  11. for (var i=0;i<a.length;i++) {
  12. if(m==a.substr(i,m.length)) {
  13. c++;
  14. }
  15. }
  16. if(c!=2)
  17. {
  18. alert("enter three keywords only");
  19. }
  20. }
  21.  
  22. </script>
  23. </head>
  24. <body>
  25. <form method="post" name="form" onSubmit="return commas();">
  26. <textarea name="a""></textarea>
  27. <input type="submit" name="submit" value="sub">
  28. </form>
  29. </body>
  30.  
  31. </html>
  32.  
i got it. thanks for ur replys. ur replys are helped a lot to me.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #7
23 Days Ago
Originally Posted by ardav View Post
Fair enough, what happens if somebody (I assume you have users entering data, not just yourself) puts a comma at the end?
Good point.
  1. if(substr_count(trim($input, ','), ',') == 2) {
  2. echo "Valid!;"
  3. }

@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:
  1. <?php
  2. $error = false; // Used later to print the error message
  3.  
  4. if(isset($_POST['a'])) {
  5. if(substr_count(trim($_POST['a'], ','), ',') != 2) {
  6. $error = true;
  7. }
  8. else
  9. {
  10. // Process your keywords here
  11. }
  12. }
  13. ?>
  14. <!DOCTYPE html>
  15. <html>
  16. <head>
  17. <title>Comma thingie</title>
  18. <script type="text/javascript">
  19. function commas()
  20. {
  21. var seperator = ',';
  22. var value = document.form.a.value;
  23. var count = 0;
  24.  
  25. // Remove trailing commas from the textarea value
  26. while(value[value.length-1] == seperator) {
  27. value = value.substr(0, value.length - 1);
  28. }
  29. document.form.a.value = value;
  30.  
  31. for (var i = 0; i < value.length; i++) {
  32. if(seperator == value.substr(i,seperator.length)) {
  33. count++;
  34. }
  35. }
  36. if(count != 2) {
  37. alert("Only 3 keywords are allowed.");
  38. return false;
  39. }
  40. else {
  41. return true;
  42. }
  43. }
  44. </script>
  45. </head>
  46. <body>
  47. <?php
  48. if($error) {
  49. echo "\t<h3 style='color: red;'>Only 3 keywords are allowed.</h3>";
  50. }
  51. ?>
  52. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form" onsubmit="return commas();">
  53. <textarea name="a"><?php echo htmlentities(@$_POST['a']); ?></textarea>
  54. <input type="submit" name="submit" value="sub">
  55. </form>
  56. </body>
  57. </html>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 111
Reputation: muralikalpana is an unknown quantity at this point 
Solved Threads: 6
muralikalpana's Avatar
muralikalpana muralikalpana is online now Online
Junior Poster
 
0
  #8
23 Days Ago
Originally Posted by Atli View Post
Good point.
  1. if(substr_count(trim($input, ','), ',') == 2) {
  2. echo "Valid!;"
  3. }

@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:
  1. <?php
  2. $error = false; // Used later to print the error message
  3.  
  4. if(isset($_POST['a'])) {
  5. if(substr_count(trim($_POST['a'], ','), ',') != 2) {
  6. $error = true;
  7. }
  8. else
  9. {
  10. // Process your keywords here
  11. }
  12. }
  13. ?>
  14. <!DOCTYPE html>
  15. <html>
  16. <head>
  17. <title>Comma thingie</title>
  18. <script type="text/javascript">
  19. function commas()
  20. {
  21. var seperator = ',';
  22. var value = document.form.a.value;
  23. var count = 0;
  24.  
  25. // Remove trailing commas from the textarea value
  26. while(value[value.length-1] == seperator) {
  27. value = value.substr(0, value.length - 1);
  28. }
  29. document.form.a.value = value;
  30.  
  31. for (var i = 0; i < value.length; i++) {
  32. if(seperator == value.substr(i,seperator.length)) {
  33. count++;
  34. }
  35. }
  36. if(count != 2) {
  37. alert("Only 3 keywords are allowed.");
  38. return false;
  39. }
  40. else {
  41. return true;
  42. }
  43. }
  44. </script>
  45. </head>
  46. <body>
  47. <?php
  48. if($error) {
  49. echo "\t<h3 style='color: red;'>Only 3 keywords are allowed.</h3>";
  50. }
  51. ?>
  52. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form" onsubmit="return commas();">
  53. <textarea name="a"><?php echo htmlentities(@$_POST['a']); ?></textarea>
  54. <input type="submit" name="submit" value="sub">
  55. </form>
  56. </body>
  57. </html>
could it possible in javascript only? why b'coz i have 2 radio buttons with samename and one textarea. if i click firstone it allows only 3keywords into textarea, and if i select second radio it allows 5keywords with above functionality what you gave to me.
Last edited by muralikalpana; 23 Days Ago at 7:10 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 111
Reputation: muralikalpana is an unknown quantity at this point 
Solved Threads: 6
muralikalpana's Avatar
muralikalpana muralikalpana is online now Online
Junior Poster
 
0
  #9
22 Days Ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Comma thingie</title>
  5. <script type="text/javascript">
  6. function commas()
  7. {
  8. var seperator = ',';
  9. var value = document.form.textarea1.value;
  10. var count = 0;
  11. var murali=window.document.form.hidden1.value;
  12. alert(murali);
  13. // Remove trailing commas from the textarea value
  14. while(value[value.length-1] == seperator) {
  15. value = value.substr(0, value.length - 1);
  16. }
  17. document.form.textarea1.value = value;
  18.  
  19. for (var i = 0; i < value.length; i++) {
  20. if(seperator == value.substr(i,seperator.length)) {
  21. count++;
  22. }
  23. }
  24. if(count != 5) {
  25. alert("Only 6 keywords are allowed.");
  26. document.form.textarea1.focus();
  27. return false;
  28. }
  29. else {
  30. return true;
  31. }
  32. }
  33. </script>
  34. <script language="javascript">
  35. function chgtx2()
  36. {
  37.  
  38. alert(window.document.getElementById('radioa').value);
  39. if(window.document.form.radio.value==9.95)
  40. {
  41. window.document.form.hidden1.value=3;
  42. }
  43. else
  44. {
  45. window.document.form.hidden1.value=6;
  46. }
  47. }
  48. </script>
  49.  
  50. </head>
  51. <body>
  52. <?php
  53. if($error) {
  54. echo "\t<h3 style='color: red;'>Only 3 keywords are allowed.</h3>";
  55. }
  56. ?>
  57. <form action=" " method="post" name="form" onSubmit="return commas();">
  58. Add 10 key words $9.95 &nbsp;
  59. <input type="radio" name="radio" id="radioa" value="9.95" onClick="chgtx2();" />
  60. <input type="hidden" name="hidden1" >
  61. &nbsp;&nbsp;(OR)&nbsp;&nbsp;
  62. Add 20 keywords $19.95 &nbsp;
  63. <input type="radio" name="radio" id="radioa" value="19.95" onClick="chgtx2();" />
  64.  
  65. <textarea name="textarea1" onChange="commas();"><?php echo htmlentities(@$_POST['a']); ?></textarea>
  66.  
  67. <input type="submit" name="submit" value="sub">
  68. </form>
  69. </body>
  70. </html>
i tried like this. radio buttons have different values. but both are alerting same value. just copy paste this code. you feel difference. using this hidden value i can use in comma() for no.of keywords enter in textarea.
Last edited by muralikalpana; 22 Days Ago at 10:38 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 111
Reputation: muralikalpana is an unknown quantity at this point 
Solved Threads: 6
muralikalpana's Avatar
muralikalpana muralikalpana is online now Online
Junior Poster
 
0
  #10
22 Days Ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Comma thingie</title>
  5. <script type="text/javascript">
  6. function commas()
  7. {
  8. var seperator = ',';
  9. var value = document.form.textarea1.value;
  10. var count = 0;
  11. var murali=window.document.form.hidden1.value;
  12. var mur=parseInt(murali)-1;
  13. alert(murali);
  14. // Remove trailing commas from the textarea value
  15. while(value[value.length-1] == seperator) {
  16. value = value.substr(0, value.length - 1);
  17. }
  18. document.form.textarea1.value = value;
  19.  
  20. for (var i = 0; i < value.length; i++) {
  21. if(seperator == value.substr(i,seperator.length)) {
  22. count++;
  23. }
  24. }
  25. if(count != mur) {
  26.  
  27.  
  28. alert('Only'+ murali+ 'keywords are allowed.');
  29. document.form.textarea1.focus();
  30. return false;
  31. }
  32. else {
  33. return true;
  34. }
  35. }
  36. </script>
  37. <script language="javascript">
  38. function chgtx2()
  39. {
  40.  
  41. alert(window.document.getElementById('radioa').value);
  42. if(window.document.getElementById('radioa').value==9.95)
  43. {
  44. window.document.form.hidden1.value=10;
  45. }
  46. else
  47. {
  48. window.document.form.hidden1.value=20;
  49. }
  50. }
  51. </script>
  52.  
  53. </head>
  54. <body>
  55.  
  56. <form action=" " method="post" name="form" onSubmit="return commas();">
  57. <input type="hidden" name="hidden1" >
  58. Add 10 key words $9.95 &nbsp;
  59. <input type="radio" name="radio" id="radioa" value="9.95" onClick="chgtx2();" />
  60.  
  61. &nbsp;&nbsp;(OR)&nbsp;&nbsp;
  62. Add 20 keywords $19.95 &nbsp;
  63. <input type="radio" name="radio" id="radioa" value="19.95" onClick="chgtx2();" />
  64.  
  65. <textarea name="textarea1" onChange="commas();"><?php echo htmlentities(@$_POST['a']); ?></textarea>
  66.  
  67. <input type="submit" name="submit" value="sub">
  68. </form>
  69. </body>
  70. </html>
run this code. u know what is the problem is der....when i am selecting second radio button value 20 is not passing to commas(); it taking only 10.
Last edited by muralikalpana; 22 Days Ago at 11:24 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC