value.length < 10 return false not working
I was using a condition onsubmit to return false if the text field didn't have more than 10 characters however it stopped submitting at all just now, I don't think I changed anything less the condition but obviously I must 开发者_StackOverflow中文版have done something.
<form class="form-new" action="insert/insert.php" method="post" onsubmit="if (document.getElementById('post-input').value.length < 10) return false;">
<textarea class="form-new" id="post-input" name="text"></textarea><br/>
<input type="hidden" name="ciudad" value="<?=$city;?>">
<input type="text" name="colegio" value="<?=$group;?>">
<input class="form-new" type="submit" value="Publicar"/>
</form>
Is there a way round? Or am I just doing it wrong? Thanks
Here is your example.
But have a look, if you return false in onsubmit the form will be sent anyway. In order to perform alert or something like that, you should do something like this:
<script type="text/javascript">
function checkLength() {
var textarea=document.getElementById('post-input');
alert(textarea.value.length >= 10);
if (textarea.value.length >= 10) {
document.getElementById('myform').submit();
}
}
</script>
<form class="form-new" action="insert/insert.php" id="myform" method="post">
<textarea class="form-new" id="post-input" name="text"></textarea><br/>
<input type="hidden" name="ciudad" value="<?=$city;?>">
<input type="text" name="colegio" value="<?=$group;?>">
<input class="form-new" type="button" value="Publicar" onclick="checkLength()"/>
</form>
and this is the link: http://jsfiddle.net/YNcU4/1/.
UPDATE: Please read comments. The original code should work.
精彩评论