How to verify a form is empty ? PHP or JS
Please, I'm a newbie in PHP, and created my web site, with a text form, i can verify with JQuery if the txt form is empty or not with the following code :
$(document).ready(function()
{
$('#formLike').ajaxForm({
target: '#content',
beforeSubmit: validateForm
});
//hide error containers
$("#txtform_error").hide();
});
function validateForm开发者_开发百科()
{
$("#txtform_error").empty().hide();
var txtform = $("#txtform").val();
var errors = 0;
if (txtform == null || txtform == '' || txtform == ' ')
{
$("#txtform_error").show().append("Please Write a Message Before Clicking : Like It");
document.formLike.txtform.focus();
}
else
{
document.formLike.submit();
}
}
How to verify that the txt form is not empty, and there is not only white-spaces in the field, because here after submitting many white-spaces, the text is posted to the action page.
Sorry for my bad english.
Thanks in advance.
Replace this line
if (txtform == null || txtform == '' || txtform == ' ')
With this line
if ($.trim(txtform) == "")
$.trim
removes unnecessary spaces so it will get trimmed to "" if someone enters only white space.
[EDITED]
for the checking:
if (txtform == null || txtform == '' || txtform == ' ')
must be
if (!txtform || txtform.replace(/\s/g, '') == '')
or
if (!txtform || !txtform.replace(/\s/g, ''))
or
if (!txtform || !jQuery.trim(txtform)) //trimmed only
This means, that the input must contain atleast 1 non-space character
精彩评论