/    Sign up×
Community /Pin to ProfileBookmark

Is it possible to have a fuction that will allow one space between words but no more?

For instance, I have a Comments field that I would like for the users to input text into. I already have checks to see whether the field is blank or not, but I was wondering whether there was a function that would allow something like:
“This is a test” but it would not allow
“This

is a test” since there is more than one white space between “This” and “is” rather than just one space?

Any help is appreciated ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@RodionGorkMar 09.2012 — [CODE]
var text = "Some text to test";
if (/s{2,}/.test(text)) {
alert("Zlodees!");
} // if
[/CODE]
Copy linkTweet thisAlerts:
@APD1993authorMar 09.2012 — [CODE]
var text = "Some text to test";
if (/s{2,}/.test(text)) {
alert("Zlodees!");
} // if
[/CODE]
[/QUOTE]


Would I have to hard code the text, or could I use something like:

[CODE]return /s{2,}/[/CODE] as a function?
Copy linkTweet thisAlerts:
@RodionGorkMar 09.2012 — Surely you need not hardcode the text being tested (it would be strange solution), but what you have written would not do (you want to return regexp object itself - why?). For function try passing the text as an argument to your function (bit obvious, yeah?):

[CODE]
function haveConsecutiveSpaces(t) {
return /s{2,}/.test(t);
} // haveConsecutiveSpaces

alert(haveConsecutiveSpaces("Here they aren't")); //alerts "false"
alert(haveConsecutiveSpaces("Here they are")); //alerts "true" (I hope)
[/CODE]


Try something like this (and I recommend to read a little from google "javascript regular expressions"). Write if I missed something.
×

Success!

Help @APD1993 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...