1

I have a textbox and I am trying to validate it in jquery. The condition is, it should not validate if user only types space bar. Currently it validates it, since the length is not equal to 0. How can I validate for empty white spaces?

var name = $('#txtName').val();
if (name.length <= 0) {
    $('#divName').css("border-color", "red");
    ShowHideById('sendEmailByCustomer', 'true');
    isValid = false;
    return;
}

2 Answers 2

2

use $.trim() to remove whitespaces:

var name = $.trim($('#txtName').val());
if (name.length <= 0) {
    $('#divName').css("border-color", "red");
    ShowHideById('sendEmailByCustomer', 'true');
    isValid = false;
    return;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use $.trim() to remove the whitespace from the beginning and end of a string as shown :-

var name = $.trim($('#txtName').val());
if (name.length <= 0) {
    $('#divName').css("border-color", "red");
    ShowHideById('sendEmailByCustomer', 'true');
    isValid = false;
    return;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.