SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    332
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error: s has no properties

    Hi,

    Will someone show me how to correctly phrase this function. The goal is to trim left and right white spaces:
    Code:
    <script type="text/javascript">
    function checkForm()
    {
        var  cmessage;
        with(window.document.msgform)
        {
            cmessage = message;
        }    
        if(trim(cmessage.value) == '')
        {
            alert('Please enter your message');
            cmessage.focus();
            return false;
        }
        else
        {  
            cmessage.value = trim(cmessage.value);
            return true;
        }
    }
    
     function trim(s)
          {
            return s.replace(/^\s*|\s*$/g, '');
          }
    </script>
    I am receiving an error: 's has no properties'

    Thanks,

    Andy

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if s has no properties, cmessage.value must be undefined.
    Get rid of the with statement, and call the element named message directly-something like
    Code:
    function checkForm(){
    	var who=document.getElementsByName('message')[0];
    	if(!who.value) return false;
    	return who.value.replace(/(^\s+)|(\s+$)/g,'');
    }

  3. #3
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    332
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I post the response but leave the message blank, the alert is triggered. That is why I am confused. The variable does have a value.

    Andy

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You only use s in one place- the trim function.
    You only call trim once-trim(cmessage.value)
    If trim(cmessage.value) === '', trim would return the empty string.

    s has no properties means the argument- cmessage.value is undefined

    undefined=='',
    but undefined is not === ''

  5. #5
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    332
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I need to digest this.

    Andy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •