简体   繁体   中英

JavaScript Form onkeyup Validation Errors

Not getting any errors in Aptana, so something I'm doing probably doesn't make sense. Basically, I am getting the value from a form and checking it against a regex. If the new checked variable isn't empty then I output to a different div that it is valid, and that it is not valid if the variable is empty.

    <script type="text/javascript">
        var age_regex=/(1[8-9]|2[0-9]|3[0-5])/;
        var error_box= document.getElementById('error_box');
            function checkAge(x){
                var age = document.getElementById(x).value;
                var checked_age = test.age_regex(age);
                if (checked_age.value != "")
                error_box.innerHTML = "Correct!";
                else {
                error_box.innerHTML = "Incorrect!";
                }
                }
    </script>

Why regex for age ? How about this :

function checkAge(str) {
    if(parseInt(str, 10) != str) {
        return false;
    }
    if(parseInt(str, 10) < 18 || parseInt(str, 10) > 35)
    {
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM