/    Sign up×
Community /Pin to ProfileBookmark

Check If Textbox contains whitespace

I am using this to check if the field is null, but it seems this does not account for whitespace.

[code]
<script type=”text/javascript”>
function ValidateData() {
var address;
address = document.getElementByID(“txtaddress”).value;
if (address == ”)
{
alert(“Error”);
return false;
}}
</script>
[/code]

Now I have read that you can use .Trim() to trim characters, but if I add a .Trim() method to my JS it errors and doesn’t even check if the address field is null?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 18.2015 — trim() ist written in lower case. This should work:
[CODE]address = document.getElementByID("txtaddress").value.trim(); [/CODE]
Copy linkTweet thisAlerts:
@SpensaurOct 20.2015 — 
and doesn't even check if the address field is null?[/QUOTE]


I think you mean "if the field is undefined", null and undefined are very different and you would never get the value as null unless you explicitly assigned it, either way null, undefined, and an empty string all evaluate to false in an if statement, so if you change you code to

<i>
</i>if (address.trim()){

}


Also, var can come on the line with you assignment to address before the variable name

you don't need var address; on a separate line like you have now
Copy linkTweet thisAlerts:
@SpensaurOct 20.2015 — 
and doesn't even check if the address field is null?[/QUOTE]


I think you mean "if the field is undefined", null and undefined are very different and you would never get the value as null unless you explicitly assigned it, either way null, undefined, and an empty string all evaluate to false in an if statement, so if you change you code to

<i>
</i>if (address &amp;&amp; address.trim()){

}


Also, var can come on the line with you assignment to address before the variable name

you don't need var address; on a separate line like you have now
×

Success!

Help @chalupabatman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...