/    Sign up×
Community /Pin to ProfileBookmark

validate for null and empty(spacebar)

I am validating my form to see if a user didn’t enter data in a text box.
The code below works if they don’t enter data, but if they click in the box and press the spacebar it does not work. I want to test for both empty and null(which is what I think the spacebar is).

I know I have to change this line to test for both, but I am lost on how to appcomplish this. any ideas?

[b]
if ( document.test.elements[err[i]].value == “” )
[/b]

Thanks

[code]
<script type=’text/javascript’>
function isEmpty()
{

var errmsg = new String();
var err = new Array(“req1”);

for(i=0;i<err.length;i++) {

if ( document.test.elements[err[i]].value == “” )
{
errmsg += “Error”;

}

}

if(errmsg.length > 0) {
alert(errmsg);
return false;
}

return true;
}

</script>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MikeyJuddJan 24.2008 — Try this.
<i>
</i>// Get the value
var elementValue = document.test.elements[err[i]].value;

// Trim any whitespace
elementValue = elementValue.trim();

if (elementValue == "") {
...
}
Copy linkTweet thisAlerts:
@magentaplacentaJan 24.2008 — Click the button with no value in the form, a space bar value, and finally any string.

[CODE]<script type="text/javascript">
function validateSpace() {
var mytextfield = document.getElementById("mytextfield").value;

if (!mytextfield) alert("no value");
else if (mytextfield.charAt("0") == " ") alert("first character IS a space");
else alert("first character is NOT a space");
}
</script>

<form>
<input type="text" id="mytextfield">
<input type="button" value="Space Present?" onclick="validateSpace();">
</form>[/CODE]
×

Success!

Help @cedtech31 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.16,
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,
)...