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.

I suggest that you 'explode' the string and then check that the parms exist in the array that is created.

Hey.

You could also use the substr_count function.

if(substr_count($input, ',') == 2) {
    echo "Valid!;"
}
Member Avatar for diafol

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?

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.

<?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>
<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>

i got it. thanks for ur replys. ur replys are helped a lot to me.

Fair enough, what happens if somebody (I assume you have users entering data, not just yourself) puts a comma at the end?

Good point.

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
$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>

Good point.

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
$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>

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.

<!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 &nbsp;
            <input type="radio" name="radio" id="radioa" value="9.95" onClick="chgtx2();" />
			<input type="hidden" name="hidden1" >
             &nbsp;&nbsp;(OR)&nbsp;&nbsp;
          Add 20 keywords $19.95 &nbsp;
          <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>

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.

<!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 &nbsp;
            <input type="radio" name="radio" id="radioa" value="9.95" onClick="chgtx2();" />
			
             &nbsp;&nbsp;(OR)&nbsp;&nbsp;
          Add 20 keywords $19.95 &nbsp;
          <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>

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.

IDs of elements don't work like the names of input elements. If you have two elements with the same ID, only one of them will be used by JavaScript. (The last one, I believe.)

So you need to rethink your approach.
A simple workaround is to have the elements pass their value into the function as a parameter and have the function use that.

Meaning, you would have to change both the radio buttons to:

<input type="radio" name="radio" value="xxxx" onclick="chgtx2(this.value);" />

And the JavaScript function to something like:

<script language="javascript">
        function chgtx2(value)
        {
            if(value==9.95)
            {
                window.document.form.hidden1.value=10;
            }
            else
            {
                window.document.form.hidden1.value=20;
            }
        }
        </script>

That should do the trick.

And to have the keyword count changed based on the radio button selection, change the JavaScript to:

if(count != parseInt(window.document.form.hidden1.value) - 1) {
    alert("Only "+ window.document.form.hidden1.value +" keywords are allowed.");
    document.form.textarea1.focus();
    return false;
}
else {
    return true;
}

And you could add the PHP code into that, like so:

<?php
$error = false; // Used later to print the error message

if(isset($_POST['a'])) {
    if(substr_count(trim($_POST['a'], ','), ',') != intval($_POST['hidden1']) - 1) {
        $error = true;
    }
    else
    {
        // Process your keywords here
    }
}
?>

Which I highly recommend, seeing as JavaScript is not really a reliable validation method.

Member Avatar for diafol

... JavaScript is not really a reliable validation method.

Ditto.

Client side validation is only ever useful to prevent MOST users from sending wrong data to the server MOST of the time. It will not prevent certain individuals from bypassing these checks (intentionally or otherwise) and you can guarantee that at some point "tainted data" will reach your server. Anyone with a webpage can send data via 'dummy form' to your form handling page.
Nasties could be in the shape of SQL/script injection - not nice.

Which I highly recommend, seeing as JavaScript is not really a reliable validation method.

could i know why java script is not reliable validation to compare with php? i will keep in my mind ur valuable suggestions.

Thats simply because javascript can perform only client side validation... a nasty user can disable javascript for his/her browser and can easily surpass ur validation script.
But php validation is done at server end which is impossible for a user to disable coz its out of his/her reach.
The reason we use javascript is to do some basic validations to avoid user sending data to server and then getting rejected from server validation which will eat up much of server's resources.
In a nutshell javascript can enhance ur validation process but can never replace server side validation(PHP in this case).
Hope i made my point clear.

could i know why java script is not reliable validation to compare with php? i will keep in my mind ur valuable suggestions.

i am passing null values for 10 keywords like----- j,j, , ,j,j,j,j,j,j. i think this is also an error. but it does not showing error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.