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,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 591 Times in 572 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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

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