0

Hey guys taking a stab at my first Javascript validation but getting stuck pretty early on. I am calling a JS function from onkeyup="" but I receive the error:

Object is not a function

Code:

<html>
    <head>
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <h3> My Sign Up Form</h3>
        <form name="signup">
            <label>First Name<input type="text" name="fname" onkeyup="fname()"/></label><div id="fnameVal"></div><br>
            <label>Last Name<input type="text" name="lastname" onkeyup="fourchars();"/></label><div id="lnameVal"></div><br>
            <!--<label>Email<input type="text" name="email"/></label><div id="email"></div><br>
            <label>Password<input type="password" name="password"/><div id="password"></div></label><br>
            <label>ConfirmPassword<input type="password" name="confirmpassword"/><div id="confirmpassword"></div></label><br>-->
            <label><input type="submit" value="Submit"/></label> 
         </form>
    </body>
    <script>    
        function fname(){
            alert('jjjj');
        }
    </script>
</html>
3
  • try wrapping your javascript code in document ready ? Commented Aug 26, 2014 at 11:32
  • You might think about putting your JavaScript before the </body> rather than the </html>. Commented Aug 26, 2014 at 11:36
  • possible duplicate of stackoverflow.com/questions/9158238/… Commented Aug 26, 2014 at 11:59

5 Answers 5

1

you can't have your function name same as your control name. i.e. fname & fname()

            <label>First Name<input type="text" name="fname" onkeyup="validateFname()"/></label><div id="fname"></div>

OK, Try something like this.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, you are right. But it seems to be a comment. Add some more explanation to your answer.
0

replace your onkeyup to this. I know it is the same code but sometimes it works when you relace you code by the same code form an other site. BTW it works fine if I test is on a site.

onkeyup="fname()"

Comments

0

Raname your function to be different from the input name.

e.g.

<script>
    function valname(){
      alert('jjjj');
 }
</script>
</head>
<body>
    <h3> My Sign Up Form</h3>
    <form name="signup">

    <label>First Name<input type="text" name="fname" onkeyup="valname()"/></label><div id="fnameVal"></div><br>

Comments

0

It seems as fname is a reference to your element in your form. Try changing the name of the function or the name of element.

function fnameFunc() {
   alert('jjjj');
}

And the HTML

<input type="text" name="fname" onkeyup="fnameFunc()"/>

Comments

0

You can use jquery on keyup

$(document).ready(function() {

$('body').on("keyup", "input[name=fname]", function() {
    alert($(this).val());
    // do your stuff
});


});

JSFIDDLE DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.