Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Passing field name through javascript?

    I want to be able to use on function to check if a field has a value typed in on keyup.

    But I do not understand how I can get the javascript to pull the value in from the HTML. Here is my JavaScript:

    Code:
    <script type="text/javascript">
    function validate_now(name) {	
    	if (document.upload.(name).value.length == 3) {	
    	alert("True");
    	return true;
    	else {
    	document.upload.(name).style.backgroundColor = "#990000";
    	return false;
    	}
    }
    </script>
    HTML:
    Code:
    <input type="text" name="imageTitle" class="inputField" 
            onkeydown="limitText(this.form.imageTitle,this.form.countdown1,30);"
            onkeyup="limitText(this.form.imageTitle,this.form.countdown1,30); validate_now('imageTitle')" />
             <input readonly type="text" name="countdown1" size="3" value="30" style="background-color:#999999; border:0px; color:#339900; font-family:Calibri; font-weight:bold;">

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,909
    Thanks
    160
    Thanked 2,230 Times in 2,217 Posts
    Code:
    validate_now(this)
    Code:
    function validate_now(obj) {	
    	if (obj.value.length == 3) {	
    	alert("True");
    	return true;
    	else {
    	obj.style.backgroundColor = "#990000";
    	return false;
    	}
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    martynball (01-18-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Still not working

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,909
    Thanks
    160
    Thanked 2,230 Times in 2,217 Posts
    Quote Originally Posted by martynball View Post
    Still not working
    Actually, what's your ultimate aim?
    onkeydown="limitText(this.form.imageTitle,this.form.countdown1,30);"
    and what's the purpose of the above code?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    The function of the first code is to display the character limit, which counts down.

    My end goal is to make the field background/border a red colour whilst the minimum number of characters is not input. Then turn green when minimum number of characters is met.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,417
    Thanks
    85
    Thanked 4,907 Times in 4,868 Posts
    And what happens if somebody uses ONLY the mouse to copy/paste code into the field???

    And do you have a max size, as well??

    I prefer to do this via a timer. Every quarter second, or so, I go out and check the length and then show the min/max (and any effects you want) results.
    Be yourself. No one else is as qualified.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,600
    Thanks
    11
    Thanked 616 Times in 596 Posts
    Quote Originally Posted by Old Pedant View Post
    And what happens if somebody uses ONLY the mouse to copy/paste code into the field???
    there is the little used onpaste event...
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com


  •  

    Posting Permissions

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