www.webdeveloper.com
Results 1 to 2 of 2

Thread: Validate field if something is in it

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Validate field if something is in it

    Hello all,

    I am trying to write some Javascript to validate a certain field. I have it working but I want to make it only validate if there is something in that field. I've looked at different tutorials online but I can't seem to get it to work. This is what I have so far:

    if((form.fieldname.value.length < 5))
    {alert("Field must have at least 5 characters!");
    return false;
    }


    I don't want this statement to run unless there is something entered into the field.Thanks.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    var val = document.forms['formname'].elements['fieldname'].value;
    if((val.length > 0) && (val.length < 5))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles