简体   繁体   English

我想知道如何在JavaScript中进行表单验证

[英]I want to know how to do form validation in javascript

I am trying to learn form validation and its not working. 我正在尝试学习表单验证,但它不起作用。

// birthyear must be between 1900 and 2012. turn the birth year textbox color to yellow
        window.onload = function() {
        document.forms[0].onsubmit = function() {
        for(i = 0; i < document.forms[0].elements.length; i++){

       var x =
       document.forms[birthyear].value

    if (x != (>1900 &&<=2012)){
    alert("Must be between 1900 and 2012");
    x.this.style.color ="yellow";
    return false;

//this is how I have created the form: //这就是我创建表单的方式:

<form action = "fake.php"></br>

    Username  
   <input class ="required"type = "text" name = "username" id ="username" /><br>
   Username
   <input class = "required"type = "text" name ="username"id ="username"/>   <br>


Birthyear
<input class  = "required" type = "number" name = "birthyear" id= "birthyear"/>

<input type = "submit"/>
</form>
if(x<1900 || x> 2012){
    alert("invalid year");

Use if statement like this and try 使用if语句,然后尝试

and check the variable x , if it is taking the value user entered correctly. 并检查变量x,如果它采用了用户正确输入的值。 Simply put alert for x variable and confirm it first 只需为x变量设置警报并先确认

Your if statement condition, x != (>1900 &&<=2012) , makes no sense. 您的if语句条件x != (>1900 &&<=2012)毫无意义。 >1900 and <=2012 do not evaluate to booleans, so you can't use the && operator on them. >1900<=2012不会得出布尔值,因此您不能在它们上使用&&运算符。 What you want is something like this: 您想要的是这样的:

x<1900 || x>2012

This checks if x is too low or too high, then uses the || 这将检查x是否太低或太高,然后使用|| (or) operator to check whether x is invalid in either way. (或)运算符,以两种方式检查x是否无效。

There are some syntax issues with your code. 您的代码存在一些语法问题。 If you want get value of the birthyear input. 如果要获取出生年份输入的值。 You don't have to iterate over elements in form (as you do using for loop), you can do so: document.forms[0].elements['birthyear'] 您不必遍历表单中的元素(就像使用for循环一样),您可以这样做: document.forms[0].elements['birthyear']

Also when you get a value of input element it type is string. 同样,当您获得输入元素的值时,其类型为字符串。 And before comparing it to a value with integer type, you should convert string to integer: 并且在将其与整数类型的值进行比较之前,应将字符串转换为整数:

intValue = parseInt(stringValue, 10);

So you code will be following 所以你的代码将跟随

<form action="fake.php">Username
    <input class="required" type="text" name="username" id="username" />Birthyear
    <input class="required" type="number" name="birthyear" id="birthyear" />
    <input type="submit" />
</form>

<script>
// birthyear must be between 1900 and 2012. turn the birth year textbox color to yellow
window.onload = function () {
    document.forms[0].onsubmit = function () {

        var birthYearElem = document.forms[0].elements['birthyear'],
            stringValue = birthYearElem.value,
            intValue = parseInt(stringValue, 10);

        if (intValue < 1900 || intValue > 2012) {
            alert("Must be between 1900 and 2012");
            birthYearElem.style.color = "yellow";
            return false;
        }
    }
}
<script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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