如何获取输入文本长度并在javascript中验证用户 [英] How to get input text length and validate user in javascript

查看:67
本文介绍了如何获取输入文本长度并在javascript中验证用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户在创建帐户时输入的用户名和密码是安全的,或者根据输入字段右侧的长度(对于我的情况5),通过显示不同颜色,即红色较短,绿色较短。我怎么能用javascript做到这一点?

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?

推荐答案

JavaScript验证并不安全,因为任何人都可以更改脚本在浏览器中的功能。使用它来增强视觉体验是可以的。

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.

var textBox = document.getElementById("myTextBox");
var textLength = textBox.value.length;
if(textLength > 5)
{
    //red
    textBox.style.backgroundColor = "#FF0000";
}
else
{
    //green
    textBox.style.backgroundColor = "#00FF00";
}

这篇关于如何获取输入文本长度并在javascript中验证用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆