SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reusable field length checker

    I am working on a little script that checks the length of a text field that I'd like to use on multiple forms. The problem that I'm running into is that I need to pass the fieldname for it to work. If I pass the name through the javascript(); link, it errors out saying "fieldname.value" is null or not an object.

    Here's what I have so far:
    Code:
    <script language=javascript>
    function checklength(theform,fieldname,maxlength) {
    if (isNaN(maxlength)=='' || isNaN(maxlength)==0) {
       maxlength=2000;
    }
    if (maxlength!= 0) {
       message = "\nThe maximum permitted length is "+maxlength+" characters.";
    }
    else { message = ""; }
    alert("Field length is "+theform.fieldname.value.length+" characters long."+message);
    }
    </script>
    
    <form action="" method=post name="pg1">
    <textarea name="objective"></textarea>
    <a href="javascript:checklength(document.pg1,'objective',2000);">[check text length]</a>
    I can get it to work without passing the field name but doing so would force me to recreate the javascript over and over for each field that I want to test.

    Is this doable?
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this

    Code:
    <HTML>
    <HEAD>
    <TITLE>test page</TITLE>
    <script language="javascript">
    function checkLength(theform, theElement, maxL){
    	if(String(document.getElementById(theElement).value).length > maxL){
    		alert("bla bla bla " + maxL)
    	}else{
    		document.getElementById(theform).submit()
    	}
    }
    </script>
    
    </HEAD>
    <BODY>
    <form name="myform" id="myform" action="checkLength.htm" method="post">
    	<input type="text" name="mytext" id="mytext" onblur="javascript:checkLength('myform','mytext','4')">
    </form>
    
    </BODY>
    </HTML>
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  3. #3
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a bunch... its working now!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  4. #4
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    no probs
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne


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
  •