-2

Currently it validates to true(passes the test) even if username contains just text, or just number characters alone. I want the validation to only be true(Passed) if and only if the username contains both text and number characters and fail otherwise.

How do I implement that? Thank you.

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;
 }
}
Nayuki
  • 17,911
  • 6
  • 53
  • 80
kereeee
  • 3
  • 2

2 Answers2

0

It seems that you want to check for a alphanumeric username without 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.The code first checks if hh is not a letter or number, if so than returns false.
2.Otherwise,
2.1.Checks for number, if present than set num to true.
2.2.Check for letter, if present than set alp to true.
3.If both num and alp is true, ie. both letters and numbers are present than return true.
4.Otherwise, returns false.

0

Your question is unclear if you would accept a mix of digits and letters /^(([A-Za-z]+\d)|(\d+[A-Za-z]))[A-Za-z0-9]*$/ or if it is strictly letters trailed by digits /^+\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 )); } );

Tests:

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
George Houpis
  • 1,729
  • 1
  • 9
  • 5