0

I want to check input's value while typing but my code seem not working (work on snippet but MVC VS 2013)
Something wrong with my code ?

$(document).ready(function(){

        $('#txtAge').keyup(function(){
            var pattern = /^\d+$/;
            if(pattern.test($(this).val()) && $(this).val()!='' ){

                $("#age_error_message").hide();
                $("#txtAge").css('border-bottom', '2px solid #34F458');
            }
            else if ($(this).val() =='') {
                $("#age_error_message").html("Please enter your age");
                $("#age_error_message").show();
                $("#txtAge").css('border-bottom', '2px solid #F90A0A');
                
            }
            else {
                $("#age_error_message").html("Should contain number only");
                $("#age_error_message").show();
                $("#txtAge").css('border-bottom', '2px solid #F90A0A');
                
            }

        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js">
</script>
Age: <input name="age" id="txtAge" type="text" class="btn-sm form-control " placeholder="Your age" />
<span class="error-form btn-sm text-danger" id="age_error_message"></span>

2

2 Answers 2

2

change like this .your code working fine

$('#txtAge').keyup(function(){
        var pattern = /^\d+$/;
        if(pattern.test($(this).val()) && $(this).val()!='' ){

            $("#age_error_message").hide();
            $("#txtAge").css('border-bottom', '2px solid #34F458');
        }
        else if ($(this).val() =='') {
            $("#age_error_message").html("Please enter your age");
            $("#age_error_message").show();
            $("#txtAge").css('border-bottom', '2px solid #F90A0A');

        }
        else {
            $("#age_error_message").html("Should contain number only");
            $("#age_error_message").show();
            $("#txtAge").css('border-bottom', '2px solid #F90A0A');

        }

    });

verified .. https://jsfiddle.net/knv8grt6/

1
  • I din't see any change in code. you have the exact copy of OP's code. Dec 27, 2017 at 19:05
0

It seem my code doesnt work bcz i put multi functions in 'script' tag . When i create new 'script' tag then put my code in, it work fine. Can someone explain ?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.