How do I prevent users from submitting this form blank?
I want to make one form don't do anything at all if what is submitted in the first field is shorter than 5 characters.
I got a code for a similar thing but it doesn't work on this form since the second field is hidden and it's filled by the page, so the form isn't blank when submitted.
So I want the fo开发者_JAVA技巧rm not do anything if when submitted what's typed on the first field is under 5 characters.
This is the form:
<form class="questionform" name="questionform-0" id="questionform-0">
<textarea class="question-box" style="width:97%;" cols="20" rows="4" id="question-box-' . $questionformid . '" name="title" onfocus="if(this.value == \'\'){this.value = \'\';}" onblur="if(this.value == \'\'){this.value = \'\';}"></textarea>
<input type="hidden" class="ubicacion" style="width:60%; font-weight:bold; border-color:white; background:white; color:white" value="<?php the_search_query(); ?> " name="question" /></p>
<div class="question-form-bottom"><input type="button" name="ask" value="Publicar" onclick="askquestion('questionform-0');"></div></form>
If it's not clear or you need more info please ask. Thanks
You really can't stop someone from doing it. You could try a JavaScript check but also someone might disable it and submit anyway. Best method is always do server side validation and just return an error like you js check would do.
you can do it client side by using Javascript or you can do it on php by validation and if validation fails display error and no redirect to the next page.
The best thing to do here is use AJAX. With AJAX, you can control the submission of the form. Basically, you make your submit button just a regular button calling an AJAX function, which will call a PHP file to validate and submit the form. Here's a great site explaining AJAX:
http://www.learn-ajax-tutorial.com/
Good luck :D
精彩评论