|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Simplest way to check for all blanks in input text field?
Is there a generic way I can test for all blanks in an input text field without looping through the entire string?
Testing for "" works great for no input but testing for " " is defeated if user enters 2 blanks. |
|
#2
|
||||
|
||||
|
Use .match() :
Code:
<html>
<head>
<SCRIPT LANGUAGE="Javascript">
function spaceCheck(someString) {
if(someString.match(/\s+/)) {
alert("Agh! No whitespace allowed!");
}
else {
alert("You're A-OK!");
}
}
</script>
</head>
<body>
<input type="text" id=fooStuff>
<button onClick="spaceCheck(fooStuff.value);">
</body>
</html>
|
|
#3
|
|||
|
|||
|
Your code worked great! I know zilch about regular expressions and had not even heard of the match method but my remorse isn't so great that I won't use the code. Thanks.
What I found online about the match method wasn't very helpful and learning about regular expressions is probably best left to a full-blown javascript book but I will look into these when I curl up with the big "O'Reilly's Javascript 4th Ed." As usual, I understand the concept but don't know enough of the implementation details to "take it and run with it". |
|
#4
|
||||
|
||||
|
Incidentally, if the string you're testing will never have any whitespace characters except actual spaces (as opposed to tabs, carriage returns, etc) you can remove the + from the pattern.
Quote:
Offhand I don't remember if this covers actual regular expressions in great detail. For that you're best off getting O'Reilly's "Mastering Regular Expressions". |
![]() |
| Viewing: Dev Shed Forums > Other > Beginner Programming > Simplest way to check for all blanks in input text field? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|