0

I'm writing a validation condition based on a variable name - test. I'm taking the textbox value to a variable- test. If user add character want to restrict the form submission. Below second OR condition will not suit for me because if user input multiple it won't work. Suggestions please...

if(test =="" || test == " "){
}
user3066595
  • 37
  • 1
  • 7

1 Answers1

2

Looks like you are trying to check if user just added white spaces and tries to submit. You can use $.trim for that

$("#b").click(function(){
    
    if($.trim($("#t").val()) === ""){
        alert("not valid")
     }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<textarea id="t"></textarea>

<button id="b">Check</button>

Even if there are multiple whitespaces $.trim will return you "". So you don't have to worry about taking care for more than one white spaces

Muhammad Usman
  • 10,039
  • 22
  • 39