【问题标题】:How do I validate a given username to only accept alphanumeric values, without using Regex如何验证给定的用户名只接受字母数字值,而不使用正则表达式
【发布时间】:2016-04-20 04:35:51
【问题描述】:

目前,即使 username 仅包含 text 或仅包含 number 个字符,它也会验证为 true(通过测试)。我希望验证只有当且仅当用户名同时包含 textnumber 字符时才为真(Passed),否则验证失败。 p>

我该如何实现呢?谢谢。

function isUser(username)
{
	var numaric = username;
	for(var j=0; j<numaric.length; j++)
	{
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if ((hh > 96 && hh<123) || (hh > 64 && hh<91) == false) { //A-Z - a-z
			
		} else if ((hh > 47 && hh<58) ==false){ //0-9
			
		} else if (true == (hh > 96 && hh<123) || (hh > 64 && hh<91) || (hh > 47 && hh<58)) { //A~Z - a~z - 1~9
		} else {
			alert("Your Alpha Numeric Test Falid");
			return false;
		}
		alert("Your Alpha Numeric Test passed");
		return true;
	}
}

【问题讨论】:

  • 问题是什么
  • 查看SO Post
  • 为什么不使用正则表达式?这是作业吗?
  • 啊哈哈不,这只是老师的挑战......我在10小时后放弃了......>,

标签: javascript html validation ascii


【解决方案1】:

您似乎想检查没有 RegExp 的字母数字用户名。 ABC
//fail
123
//fail
abc123
//pass
abc123$_
//fail

function isUser(username)  
{  
var numaric = username;  
var num, alp;  
for(var j=0; j<numaric.length; j++)  
{  
var alphaa = numaric.charAt(j);  
var hh = alphaa.charCodeAt(0);  
if(!((hh>=48&&hh<=57)||(hh>=97&&hh<=122)||(hh>=65&&hh<=90))) //if hh is anything other than letter or number return false  
{  
alert('Test failed. Non-alphanumeric characters are present.');  
return false;  
}  
else  
{  
if(hh>=47&&hh<=57) //if number  
num=true;  
if((hh>=97&&hh<=122)||(hh>=65&&hh<=90)) //if letter  
alp=true;  
}  
}  
if(num&&alp) //if both letter and number is present than passed  
{  
alert('Test passed');  
return true;  
}  
else  
{  
alert('Test failed. Not alphanumeric (contains only letters or numbers).');  
return false;  
}  
}

1.代码首先检查hh是否不是字母或数字,如果不是则返回false。
2.否则,
2.1.检查数字,如果存在,则将 num 设置为 true。
2.2.检查字母,如果存在则将 alp 设置为 true。
3.如果num和alp都为真,即。字母和数字都存在而不是返回 true。
4.否则返回false。

【讨论】:

    【解决方案2】:

    您的问题不清楚您是否会接受数字和字母的混合 /^(([A-Za-z]+\d)|(\d+[A-Za-z]))[A-Za- z0-9]*$/ 或者如果是严格的以数字结尾的字母 /^+\d+$/。

    // Matches /^[A-Za-z]+\d+$/
    function isUser(username) {
      var numaric = username;
      var mode = 0;
      for (var j = 0; j < numaric.length; j++) {
        var alphaa = numaric.charAt(j);
        var hh = alphaa.charCodeAt(0);
        var letter = ((hh > 96 && hh < 123) || (hh > 64 && hh < 91));
        var digit = (hh > 47 && hh < 58);
        switch( mode )
        {
        case 0:
            if( letter )
            break;
          else if( j == 0 )
            return false;
          ++mode;
        case 1:
          if( !digit )
            return false;
        }
     }
     return mode == 1;
    }
    
    // Matches /^(([A-Za-z]+\d)|(\d+[A-Za-z]))[A-Za-z0-9]*$/
    function isUser2(username) {
      var numaric = username;
      var found_letter = false;
      var found_digit = false;
      for (var j = 0; j < numaric.length; j++) {
        var alphaa = numaric.charAt(j);
        var hh = alphaa.charCodeAt(0);
        var letter = ((hh > 96 && hh < 123) || (hh > 64 && hh < 91));
        var digit = (hh > 47 && hh < 58);
        if((hh > 96 && hh < 123) || (hh > 64 && hh < 91))
            found_letter = true;
        else if(hh > 47 && hh < 58)
          found_digit = true;
        else
          return false;
     }
     return found_letter && found_digit;
    }
    
    var tests = ['test', '0030342', 'foobar4342', '31313ffefe', 'dfdf424dfdg']
    tests.forEach( function(d) { console.log( 'isUser( ' + d + ' ) --> ' + isUser( d )  + ' compared to ' + /^[A-Za-z]+\d+$/.test( d ) ); } );
    tests.forEach( function(d) { console.log( 'isUser2( ' + d + ' ) --> ' + isUser2( d ) + ' compared to ' + /^(([A-Za-z]+\d)|(\d+[A-Za-z]))[A-Za-z0-9]*$/.test( d )); } );
    

    测试:

    isUser( test ) --> false compared to false
    isUser( 0030342 ) --> false compared to false
    isUser( foobar4342 ) --> true compared to true
    isUser( 31313ffefe ) --> false compared to false
    isUser( dfdf424dfdg ) --> false compared to false
    isUser2( test ) --> false compared to false
    isUser2( 0030342 ) --> false compared to false
    isUser2( foobar4342 ) --> true compared to true
    isUser2( 31313ffefe ) --> true compared to true
    isUser2( dfdf424dfdg ) --> true compared to true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      相关资源
      最近更新 更多