JSFiddle - React, Tailwind, and code Playground

by George

JavaScript

// 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 )); } );