By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Basic validation question

P: 1
Hi all, I have a text area that can contain no less than 130 characters. The javascript validation I have is as follows:

[HTML]<script type="text/javascript">
function validate(field)
{
var chars = document.getElementById(field).value;
if (chars.length < 130)
{
alert("The news story content must be a minimum of 130 characters to properly display. Please enter more content and re-submit the form.");
return false;
}
else
{
return true;
}
}
</script>[/HTML]

The HTML in the form reads:
[HTML]<form action="datainsert.php?type=news" method="post" name="addNews" onsubmit="return validate(content)">[/HTML]

When I test the form with less than 130 characters in the textarea, I get the warning, but it continues on and posts data from the form. How can I stop the form from posting and returning the user to correct the error?

Thanks, C
Jul 27 '08 #1
Share this Question
Share on Google+
1 Reply


RamananKalirajan
100+
P: 607
Hai Dude, I hope this code will help you out. Just try this.

[HTML]</html>
<head>
<script type="text/javascript">
function validate()
{
var chars = document.getElementById('field').value;
if (chars.length < 130)
{
alert("The news story content must be a minimum of 130 characters to properly display. Please enter more content and re-submit the form.");
return false;
}
else
{
return true;
}
}
</script>
</head>
<form action="demo.html" method="post" onSubmit="return validate()">
<textarea id="field">
</textarea>
<input type="submit" value="Submit Me">
</form>
</html>[/HTML]

The problem in your code is in document.getElementById() you hevent place the field within quotes.

Regards
Ramanan Kalirajan
Jul 28 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.