简体   繁体   English

如何使用JavaScript验证表单字段?

[英]How to use javascript to validate form field?

Noob, trying to create a simple form, and validate the inputs on same. Noob,试图创建一个简单的表单,并验证相同的输入。 However, I don't know how to properly select each input in js, so nothing is happening. 但是,我不知道如何正确选择js中的每个输入,因此什么也没有发生。 I am just learning html, bootstrap and javascript, so simpler (pythonic) answers are preferred to more complex ones. 我只是在学习html,bootstrap和javascript,因此,较简单的(pythonic)答案优先于较复杂的答案。

I've read the documentation, and a number of other stackoverflow posts on this exact topic, which would have likely answered my question, were I not a Noob. 我已经阅读了文档,并且在这个确切的主题上有许多其他stackoverflow帖子,如果我不是Noob,可能会回答我的问题。

<div class="form-group"> 
        <label for="first_name">First Name</label>
        <input autocomplete="off" autofocus="" class="form-control" name="first_name" placeholder="First Name" type="text">
        <small id="first_name_Help" class="form-text text-muted">* First Name is Mandatory.</small>
    </div>

    <div class="form-group"> 
        <label for="last_name">Last Name</label>
        <input autocomplete="off" autofocus="" class="form-control" name="last_name" placeholder="Last Name" type="text">
        <small id="last_name_Help" class="form-text text-muted">* Last Name is Mandatory.</small>
    </div>

    <p>Select Your Country of Residence Below</p>

    <div class="form-group">
        <select name="country">

            <option disabled selected value="">Country</option>
            <option value="Canada">Canada</option></option>
            <option value="USA">USA</option></option>
            <option value="Mexico">Mexico</option>
            <option value="None of the Above">None of the Above</option>
        </select>
    </div>

    <script>

    document.querySelector('form').onsubmit = function() {
        if (!document.querySelector('input.first_name').value) {
            alert('You must provide your name!');
            return false;
        }

Your selector is wrong: 您的选择器错误:

document.querySelector('input.first_name').value

You're selecting an input element with a "first_name" class . 您正在选择具有“ first_name” input元素。 Your input class name is "form-control" and also it's not unique (which is fine). 您输入的类名称是“ form-control”,它也不是唯一的(可以)。 You must select using their name attributes. 您必须使用它们的name属性进行选择。 Like this: 像这样:

document.querySelector('input[name=first_name]').value

https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors#Selectors_article_overview https://developer.mozilla.org/zh-CN/docs/Learn/CSS/Introduction_to_CSS/Selectors#Selectors_article_overview

You can validate a form with Javascript or as an alternative use the built-in validation of HTML5 . 您可以使用Java验证表单,也可以使用HTML5内置验证 In the snipppet below I'm using the "required" keyword to make an input field mandatory. 在下面的代码段中,我使用“ required”关键字将输入字段设为必填项。

 input:invalid { border: solid red; } input:valid { border: solid green; } 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" /> <div class="form-group"> <label for="first_name">First Name</label> <input autocomplete="off" autofocus="" class="form-control" name="first_name" placeholder="First Name" type="text" required> <small id="first_name_Help" class="form-text text-muted">* First Name is Mandatory.</small> </div> <div class="form-group"> <label for="last_name">Last Name</label> <input autocomplete="off" autofocus="" class="form-control" name="last_name" placeholder="Last Name" type="text" required> <small id="last_name_Help" class="form-text text-muted">* Last Name is Mandatory.</small> </div> <p>Select Your Country of Residence Below</p> <div class="form-group"> <select name="country"> <option disabled selected value="">Country</option> <option value="Canada">Canada</option> </option> <option value="USA">USA</option> </option> <option value="Mexico">Mexico</option> <option value="None of the Above">None of the Above</option> </select> </div> <button>Submit</button> 

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

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