By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

trim spaces and check for empty form elements

P: 25
I'm trying to very that the user actually entered something in the form, and not just spaces.

I guess the problem is in the first line of isBlank() function. I've tried the following:
  • elem.value.trim();
  • elem.value=elem.value.trim();
  • elem.value=trim(elem.value);
  • elem.value.replace(/^\s+|\s+$/g,"");
and none works. It correctly gives me an error message if I leave the field completely blank.But if there's anything in the field, it does not trim away the spaces.

I found the trim functions somewhere on the web, so I'm guessing they are probably fine.

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. function check_myForm(){
  2.     if(isBlank(myForm.ofc, 'Please enter ofc:')){
  3.         return false;
  4.     }
  5. }
  6.  
  7. function isBlank(elem, helperMsg) {
  8.     elem.value.replace(/^\s+|\s+$/g,"");
  9.     if(elem.value.length == 0){
  10.         alert(helperMsg);
  11.         elem.focus();
  12.         return true;
  13.     }
  14.     return false;
  15. }
  16.  
  17. String.prototype.trim = function() {
  18.     return this.replace(/^\s+|\s+$/g,"");
  19. }
  20. String.prototype.ltrim = function() {
  21.     return this.replace(/^\s+/,"");
  22. }
  23. String.prototype.rtrim = function() {
  24.     return this.replace(/\s+$/,"");
  25. }function ltrim(str) { 
  26.     for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
  27.     return str.substring(k, str.length);
  28. }
  29. function rtrim(str) {
  30.     for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
  31.     return str.substring(0,j+1);
  32. }
  33. function trim(str) {
  34.     return ltrim(rtrim(str));
  35. }
  36. function isWhitespace(charToCheck) {
  37.     var whitespaceChars = " \t\n\r\f";
  38.     return (whitespaceChars.indexOf(charToCheck) != -1);
  39. }
Sep 29 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
If you're trying to replace, you'd have to set the value of elem to the new value:
Expand|Select|Wrap|Line Numbers
  1. elem.value = elem.value.replace(...);
Alternatively, remove that line and just use the trim() method (that you've added to String):
Expand|Select|Wrap|Line Numbers
  1. if(elem.value.trim().length == 0){
Sep 29 '08 #2

P: 25
Thanks for your response.

I tried your suggestion:
Expand|Select|Wrap|Line Numbers
  1. function isBlank(elem, helperMsg) {
  2.     if(elem.value.trim().length == 0){
  3.  
but it doesn't change the field.

Meanwhile I tried this one again, just for laughs:
Expand|Select|Wrap|Line Numbers
  1. function isBlank(elem, helperMsg) {
  2.     elem.value=trim(elem.value);
  3.     if(elem.value.length == 0){
  4.     ...
  5.  
and now it suddenly works exactly as I wanted it. Maybe I forgot a ; or something stupid like that when I first tried it.

But I'm still puzzled at why it doesn't work with string's member function.
Sep 29 '08 #3

acoder
Expert Mod 15k+
P: 16,027
If you want to change the value, then you would have to set elem.value as you've done. trim() will work if you use it on the value:
Expand|Select|Wrap|Line Numbers
  1. elem.value = elem.value.trim();
Sep 29 '08 #4

P: 25
Yup, that works.

Didn't work 2-3 hours ago - before I had my coffee. I guess lack of caffeine caused me to do some typos, such as forgetting a semicolon.

By the way, this was me very first JavaScript. Yaaay, I can do JavaScript now!!! Need to add that to my resume :)

Thanks for your help!!!!
Sep 29 '08 #5

acoder
Expert Mod 15k+
P: 16,027
Well done! Since you're new to JavaScript, I would suggest you try going through some of the links in the Offsite Links sticky thread.
Sep 29 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.