trim spaces and check for empty form elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marjeta
    New Member
    • Sep 2008
    • 25

    trim spaces and check for empty form elements

    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.repl ace(/^\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:
    Code:
    function check_myForm(){
    	if(isBlank(myForm.ofc, 'Please enter ofc:')){
    		return false;
    	}
    }
    
    function isBlank(elem, helperMsg) {
    	elem.value.replace(/^\s+|\s+$/g,"");
    	if(elem.value.length == 0){
    		alert(helperMsg);
    		elem.focus();
    		return true;
    	}
    	return false;
    }
    
    String.prototype.trim = function() {
    	return this.replace(/^\s+|\s+$/g,"");
    }
    String.prototype.ltrim = function() {
    	return this.replace(/^\s+/,"");
    }
    String.prototype.rtrim = function() {
    	return this.replace(/\s+$/,"");
    }function ltrim(str) { 
    	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
    	return str.substring(k, str.length);
    }
    function rtrim(str) {
    	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
    	return str.substring(0,j+1);
    }
    function trim(str) {
    	return ltrim(rtrim(str));
    }
    function isWhitespace(charToCheck) {
    	var whitespaceChars = " \t\n\r\f";
    	return (whitespaceChars.indexOf(charToCheck) != -1);
    }
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    If you're trying to replace, you'd have to set the value of elem to the new value:
    Code:
    elem.value = elem.value.replace(...);
    Alternatively, remove that line and just use the trim() method (that you've added to String):
    Code:
    if(elem.value.trim().length == 0){

    Comment

    • Marjeta
      New Member
      • Sep 2008
      • 25

      #3
      Thanks for your response.

      I tried your suggestion:
      Code:
      function isBlank(elem, helperMsg) {
      	if(elem.value.trim().length == 0){
      but it doesn't change the field.

      Meanwhile I tried this one again, just for laughs:
      Code:
      function isBlank(elem, helperMsg) {
      	elem.value=trim(elem.value);
      	if(elem.value.length == 0){
      	...
      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.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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:
        Code:
        elem.value = elem.value.trim();

        Comment

        • Marjeta
          New Member
          • Sep 2008
          • 25

          #5
          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!!!!

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            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.

            Comment

            Working...