0

I am trying to validate a form's input value after clicking a button using jQuery and javascript.

Right now, if I put in a value for the input and click submit the input field and button just disappears. Any idea what I have wrong here?

The HTML file...

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link/>
        <script type='text/javascript' src='script.js'></script>
    </head>
    <body>
            <form>
                <input type="text" id='input_1'>
                <input type="submit" value="Send" id="send">
            </form>   
    </body>
</html>

The script file...

$(document).ready(function() {
    $('.send').click(function() {
        validateInput(document.getElementById('#input_1'));
        function validateInput(inputValue){
             if (inputValue === 3) {
             alert("Element cannot be equal to 3");
             }
        }
    });
});
8
  • Best jQuery Form Plugin. Easily validate anything useing "beforeSubmit" option Commented May 6, 2013 at 15:10
  • click(function(event) { event.preventDefault(); will stop executing form submit Commented May 6, 2013 at 15:10
  • Additional do not use click(). Please use on('click',... Commented May 6, 2013 at 15:11
  • First is you do not prevent it will send request, second is more semantic. Commented May 6, 2013 at 15:14
  • You are right, but most programmers I know and discuss this problem prefer on. Only because this is more readable than $('button').click for example. Commented May 6, 2013 at 15:20

3 Answers 3

1
$(document).ready(function() {
    $('#send').live('click',function() { //Because send is the id of your button
        if(validateInput(document.getElementById('#input_1'))){
            alert("Element cannot be equal to 3");
            return false;
         }
         else{
             //Do something else
         }
        function validateInput(inputValue){
             if (inputValue == 3) {
             return false;//Will prevent further actions
             }
        }
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

I see I was using . instead of # for the send button. I also included the 'return false;//Will prevent further actions' as you recommended. The input and button are still disappearing after hitting send. Any additional thoughts?
Use .on() or .delegate() instead of .live(). Live is deprecated!
The return false must be inside the click function, not validateInput().
Or call validateInput like that : return validateInput(arg) and keep your return false in the function.
This prevented the input and button from disappearing but if I enter 3 the alert isn't executing.
0
  1. this $('.send').click(function() { should be $("#send").click(function() {. Notice the Hash symbol and not the dot (.), dot is used if the selector is a class whereas the # is used if the selector is an ID.

2.You should move the validation function outside the $(document).ready(function().. Hope that helps

Comments

0

Thank you all for your responses. Unfortunately, I wasn't able to get it to work using your recommendations. I decided to use jQuery to get the element and appended a message to a div instead of using alert. This was a different approach but ended up working.

HTML...

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link/>
        <script type="text/javascript" src="script.js"></script>
    </head>
    <body>
            <form>
                <input type="text" name="input"/>

            </form>
            <button id="send">Send</button>
            <div class="error">
            </div>
    </body>
</html>

SCRIPT...

$(document).ready(function() {
    $('#send').click(function(){ 
    var toValidate = $('input[name=input]').val();
    if (toValidate == 3) {
    jQuery('.message').html('');
        $('.error').append('<div class="message">' + toValidate + " is    invalid" + '</div>');
    }
    else {
        jQuery('.message').html('');
        $('.error').append('<div class="message">' + toValidate + " is valid" + '</div>');
         }

    });
}); 

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.