简体   繁体   English

如何使用 javascript 索引来自 HTML 表单的输入?

[英]How can I use javascript to index input from a HTML form?

I have a form where you must enter a username, but the username cannot start or end with a period (.).我有一个表单,您必须在其中输入用户名,但用户名不能以句点 (.) 开头或结尾。 This is what I have so far, I feel like I'm close.这是我到目前为止所拥有的,我觉得我很接近。 I think I am making an error in the.value[0] parts.我想我在 .value[0] 部分出错了。

    //Checking Username
    if (document.getElementById("uName").value[0] == "." || document.getElementById("uName").value[-1]) {
        document.getElementById("notification4").innterHTML ="Cannot have period at start or end.";
        submitForm = false;
    } else {
        document.getElementById("notification4").innerHTML="";
    }

My second question is how would I be able to stop the same character from repeating twice in a row?我的第二个问题是如何阻止同一个字符连续重复两次? For example you can't have (--), (//), (%%), (**) etc. I would prefer a similar method to use like above or with regex.例如,您不能使用 (--)、(//)、(%%)、(**) 等。我希望使用类似的方法来使用上述方法或正则表达式。

This is the forms HTML:这是 forms HTML:

        <label for="uName"> Username: </label><br>
        <input type="text" id="uName" name="uName"> <br>
        <div class= "error" id="notification4"></div><br>

You can use regular expression and the RegExp.prototype.test() function:您可以使用正则表达式和RegExp.prototype.test() function:

const regex = /^[.]|[.]$|[^a-zA-Z0-9]{2}/g;
if(regex.test(str)) {
    //code when it matches
} else {
    //code when it doesn't match
}

This checks if the first or last character is a dot (^[.]|[.]$) and if there is any character repeated twice that is not a letter or number ([^a-zA-Z0-9]{2}).这将检查第一个或最后一个字符是否为点 (^[.]|[.]$) 以及是否有任何重复两次的字符不是字母或数字 ([^a-zA-Z0-9]{2 })。

暂无
暂无

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

相关问题 我如何在javascript函数中使用来自html表单的输入? - how can i use an input from html form in my javascript function? 我可以使用 JAVASCRIPT/jQuery 将表单中的输入保存到 HTML 中的 .txt,然后使用它吗? - Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it? 如何从HTML表单获取用户输入并将其存储在现有的JavaScript数组中 - How can I get the user input from a HTML form and store it in an existing JavaScript array 如何在单击提交按钮时从 HTML 获取表单输入文本,以便我可以在 API 有效负载中使用该变量? - How do I get form input text from HTML on the click of the submit button, so I can use that variable in an API payload? 如何将javascript的值传递给html表单上的php? - How can I pass the value from a javascript to php on an html form? 如何使用从PHP到JavaScript的输入Box ID? - How can I use input Box ID from PHP to JavaScript? 在表单 action= 中使用 javascript 和 html 表单输入 - use javascript and html form input in form action= 当我更改输入时,如何在 HTML 表单上自动显示 JavaScript function 的结果? - How can I automatically show the result of a JavaScript function on a HTML form, when I change the input? HTML JavaScript如何创建此表单? - HTML JavaScript How can I create this Form? 如何使用Javascript使用表单的一个输入字段显示其余内容? - How can I use Javascript to use one input field of a form to show the rest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM