views:

180

answers:

3

Hello I am using jquery validation plugin for form validation. I have added a custom method to validate a date in the custom format. But somehow if users don't type anything in that date textbox, it doesn't give any error. Does anybody know how to check for blank or empty value?

EDIT : May be I didn't frame my question correctly. I want to show error message when user is in textbox for typing the value and he doesn't type anything. Onblur kind of event.

Thanks

A: 
var textBox = document.getElementById('textbox');

if (textBox.value === '')
    alert('We have an empty value');

or using jQuery syntax

var textBox = $('#textbox');

if (textBox.val() === '')
    alert('We have an empty value');
Russ Cam
well, since I am using a custom method in validation plugin, I doubt I can try this and even this looks similar to what Teja suggested.
yogsma
A: 

You have to give it the class "required" as well.

EDIT: adding the "required" class lets the validation plugin know this is required and will check for it to not be empty.

ocdcoder
Somehow this looks easier, but complicated for my situation as I am using JSP code.
yogsma
A: 
 $.validator.addMethod('date', function(dateval, element){
    return Utils.isBlank(dateval) || (validateDate( dateval));
 }, 'Please enter a valid date in mm/dd/yyyy format (eg; 12/31/2009).');

You have to check for blank too and the isBlank method is something like this

isBlank: function(val) {
    val = $.trim(val);
    return val == undefined || val == '';
}

Also the validateDate should be your custom date validation

Teja Kantamneni
I tried this. It doesn't work.
yogsma
Which part is not working? And what is the error?
Teja Kantamneni
Well it didn't give me any error message when I tried that. I have edited my question, I need onblur event for this jquery function actually.
yogsma