3

I have a email textbox in my web page. When user submits the page I am doing some client side validation using javascript. I can get the value of the textbox and compare it to know if it has any data or not. But in case user enters only blank spaces how will I bypass it.

I am using below mentioned code.

// Check the format of email only if it has some data.
// otherwise no checking is needed.
if ($('#txtEmail').val() != "")
{
     // do something
}

Thanks in advance.

3
  • if ($('#txtEmail').val().length) this would do too... Commented Jul 19, 2012 at 9:15
  • 1
    Thanks all for the responses. .length is not working. When more than one space is entered then .length returns the number of spaces. Trim is useful. Commented Jul 19, 2012 at 9:29
  • well you can use the trim first then .length :) if ($.trim($('#txtEmail').val()).length) Commented Jul 19, 2012 at 9:31

2 Answers 2

13

Use $.trim() function to remove all white-space from beginning and end of the string

if ($.trim($('#txtEmail').val()) != "")
{
     // do something
}
Sign up to request clarification or add additional context in comments.

1 Comment

.trim() should not be used that way... it causes problems on one of the old browsers :) if ($.trim($('#txtEmail').val()) != "") instead :)
3

javascript

if((document.getElementById('bereich').value).length==0)

or jquery

if ($('#txtEmail').val()).length==0)

or

  if ($.trim($('#txtEmail').val()).length==0)

1 Comment

@AnkitGautam - for your kind info that y i used lenght....and anyway i updated answer with trim also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.