简体   繁体   English

Javascript 代码对输入字段中输入的用户名执行 split()

[英]Javascript code to perform split() on user names entered in input field

I have a field in my HTML form where user is supposed to enter his full names.我的 HTML 表单中有一个字段,用户应该输入他的全名。 I need javascript code that can invoke the .split function to ensure there is at least a space between two words entered in the field to validate.我需要可以调用.split function 的 javascript 代码,以确保在字段中输入的两个单词之间至少有一个空格进行验证。 The split function should count the number of spaces between words, if it is zero then user has entered a single name word and should show error, if its one and above then user is good as validated.拆分 function 应计算单词之间的空格数,如果为零,则用户输入了单个名称单词并应显示错误,如果其为一个及以上,则用户已通过验证。 This one busted my nuts wide open, can anyone help me with the code这个让我大吃一惊,谁能帮我写代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="container">
    <div class="form">
        <label class="accent-1">Enter Your Full Names</label>
        <input type="text" class="form-control" id="mytext">
    </div>
</div>
<script>
    //we are going to scan if user attempted to enter one name using split
    //get the field
    let field=document.getElementById("mytext");
    let y=field.value;
    //javscript split function to fetch substrings into array and check for spaces

</script>
</body>

You could grab the value and split the string(value) and checks for the length.您可以获取value拆分字符串(值)并检查长度。 check accordingly to the condition.根据情况进行检查。

 const button = document.querySelector("button"); const input = document.querySelector("input"); const error = document.querySelector("div#error"); button.addEventListener("click", e => { const data = input.value.split(" "); if (data.length <= 1) { error.textContent = "Error"; // Do when there is only single word - REJECTED CASE } else { error.textContent = ""; // Do when there is more than a word - ACCEPTED CASE } })
 div#error{ color: red; font-size: 12px; }
 <div class="container"> <div class="form"> <label class="accent-1">Enter Your Full Names</label> <input type="text" class="form-control" id="mytext"> <div id="error"></div> <button> submit </button> </div> </div>

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

相关问题 根据用户输入执行JavaScript代码 - Perform javascript code based on user input 用于验证用户输入正确信息的Javascript代码 - Javascript code to verify user entered the right information 如何使用react和javascript检查用户在输入字段中输入的输入是否具有格式为“10h”或“10m”的字符串? - How to check if the user entered input in input field has the string in format '10h' or '10m' using react and javascript? 获取用户输入以在javascript中执行计算 - Take User input to perform calculation in javascript Spotfire:如何在输入字段中修改URL(在用户输入之后) - Spotfire : How to modify an URL in an input field (after being entered by the user) 输入两个输入字段值时,javascript函数不起作用 - javascript function does not work when two input field values are entered 只要有用 javascript 输入的文本,就改变输入字段的背景颜色? - Change background color of input field as long as there is text entered with javascript? 如何触发Span元素填充在“输入”字段(Javascript)中输入的值 - How to trigger the Span element to populate with the value entered in Input field (Javascript) 拆分输入框中输入的客户名称 - Split customernames entered in input box 输入检查用户输入的值是否介于0-9 Javascript之间 - Input check that user entered a value between 0-9 Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM