/    Sign up×
Community /Pin to ProfileBookmark

regexp: How to check if only spaces were entered in a form

Before sending a form, how can I check if only spaces were entered? That way I can prevent it from sending.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledMay 11.2009 — basically you use the [b]onsubmit[/b] event and have a function to validate the form. when the function detect a no valid data the function return boolean false to prevent for submission.
<i>
</i>&lt;form onsubmit="return checkform(this);"&gt;
&lt;input type='text' name='user' /&gt;
...
&lt;/form&gt;

function checkform(form){
if(!/^[a-z0-9]{3,}$/i.test(form.user.value))return false;
return true;
}

in this sample if there is no at least a sequence of three or more alphanumeric, the form doesn't submit.
Copy linkTweet thisAlerts:
@enchanceauthorMay 11.2009 — Sorry, I find this hard to understand. I apologize, I haven't tried regexp yet. I want to know if only spaces were entered regardless of how many there are. that way I can prevent the form from sending. Any other character is ok.
Copy linkTweet thisAlerts:
@ZeroKilledMay 11.2009 — well, the code is almost similar. however, regular expression is the more easier way to do it:
<i>
</i>function checkform(form){
return !/^s+$/.test(form.user.value);
}


if you want to achieve it without regular expression, then you will have to check each char of the string, one by one, and match it to verify if it is a white space.
×

Success!

Help @enchance 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.8,
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,
)...