如何在javascript中验证数字和大写字母 [英] How to validate number and capital letter in javascript

查看:112
本文介绍了如何在javascript中验证数字和大写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证密码:


  • 包含至少1个数字

  • 至少包含1个大写字母(大写)

  • 包含至少1个普通字母(小写)

I使用此代码

function validate()
{   
    var a=document.getElementById("pass").value
    var b=0
    var c=0
    var d=0;
    for(i=0;i<a.length;i++)
    {
        if(a[i]==a[i].toUpperCase())
            b++;
        if(a[i]==a[i].toLowerCase())
            c++;
        if(!isNaN(a[i]))
            d++;
    }
    if(a=="")
    {
        alert("Password must be filled")
    }
    else if(a)
    {
        alert("Total capital letter "+b)
        alert("Total normal letter "+c)
        alert("Total number"+d)
    }   
}

让我感到困惑的一件事是,如果我输入一个数字,它也是算作大写字母???

One thing that make me confuse is why if I input a number, it also count as uppercase letter???

推荐答案

1.toUpperCase ==1!你怎么说:)
你可以这样检查:

"1".toUpperCase == "1" ! What do you say about that :) You could do your checking like this:

for(i=0;i<a.length;i++)
    {
        if('A' <= a[i] && a[i] <= 'Z') // check if you have an uppercase
            b++;
        if('a' <= a[i] && a[i] <= 'z') // check if you have a lowercase
            c++;
        if('0' <= a[i] && a[i] <= '9') // check if you have a numeric
            d++;
    }

现在,如果b,c或d等于0,则表示存在问题。

Now if b, c, or d equals 0, there is a problem.

这篇关于如何在javascript中验证数字和大写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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