Javascript 如何在javascript中获取输入文本长度并验证用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2825144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get input text length and validate user in javascript
提问by Crazy boy
I want to valid if user has entered username and password at the time of creating an account is safe or not based on its length(for my case five) on the right side of input field by showing that in different color i.e. for shorter in red, otherwise in green. How can I do that with javascript?
我想验证用户在创建帐户时输入的用户名和密码是否安全基于输入字段右侧的长度(对于我的情况为 5),通过显示不同的颜色,即红色较短,否则为绿色。我怎么能用javascript做到这一点?
回答by filip-fku
JavaScript validation is not secure as anybody can change what your script does in the browser. Using it for enhancing the visual experience is ok though.
JavaScript 验证并不安全,因为任何人都可以更改您的脚本在浏览器中的操作。使用它来增强视觉体验是可以的。
var textBox = document.getElementById("myTextBox");
var textLength = textBox.value.length;
if(textLength > 5)
{
//red
textBox.style.backgroundColor = "#FF0000";
}
else
{
//green
textBox.style.backgroundColor = "#00FF00";
}

