double white space
I have a html form that allows text input. I perform various checks for allowances, start with letter, letters/numbers only etc.
I want to determine if there is a double space anywhere in the string, at the start, end or somewhere in the middle.
I have code to remove double space but want to send the user back to the form and inform them, rather than simply change what they typed without the option to type something else instead.
Cheers
decibel.places posted this at 23:54 — 8th September 2008.
He has: 1,494 posts
Joined: Jun 2008
wouldn't this work?
<script type="text/javascript">
if(yourstring.indexOf(" ") || (yourstring.substring(0,2) == " ")){
alert("you typed a doublespace!");
getelementbyid('formelementid').focus();
}
</script>
the substring test is if the string starts with the doublespace...
checked it:
<html>
<body>
<script type="text/javascript">
var str = " Hello world!";
if (str.indexOf(" ") || str.substr(0,2) == " ") alert('bingo');
</script>
</body>
</html>
greg posted this at 15:04 — 10th September 2008.
He has: 1,581 posts
Joined: Nov 2005
Thanks, but I was hoping for a PHP method (I should have perhaps stated that).
I can't use JS for security checking as my form checking scripts don't issue any output to the users browser, just determine if all ok do else send back to form with error.
Thanks anyway though
Greg K posted this at 19:23 — 10th September 2008.
He has: 2,145 posts
Joined: Nov 2003
Try this:
if (preg_match('/\s{2}/',$_POST['inputname'])) {
echo "There was muliple whitespaces together";
}
else {
echo "You are good to go";
}
the \s tells it any whitespace character, the {2} tells it repeated twice.
-Greg
greg posted this at 21:40 — 10th September 2008.
He has: 1,581 posts
Joined: Nov 2005
Fantastic! Works like a .. well, a bit of php code that works.
Thanks Greg.
(thanks debical too, your solution would have been great had I been able to use JS)
decibel.places posted this at 01:46 — 11th September 2008.
He has: 1,494 posts
Joined: Jun 2008
Yeah, I am working on a site that has to be section 508 accessible, therefore NO JS - but for quick client stuff, usually I go with JS (keep the server reqs down)
Greg K posted this at 02:03 — 11th September 2008.
He has: 2,145 posts
Joined: Nov 2003
IMO, if you have time to do it, have both Client-Side and Server-Side
-Greg
greg posted this at 16:24 — 11th September 2008.
He has: 1,581 posts
Joined: Nov 2005
Client code can be changed by clients, server side can only be changed by hackers who would likely get into your server anyway, regardless.
Which is why I have form validation done with PHP. Regardless of what, or how, they send data to the process page, the PHP ensures it is all acceptable data.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.