ThieveKan
ThieveKan

Reputation: 1

How to validate the textbox first character is Alpha and second character numeric in javascript?

Basically I validate account number starting with uppercase character and second character is numeric.

function CheckDecimal(inputtxt) 
{ 
    var Upper  = /^[A-Z]/; 
    var number = /^[0-9]/;
    if (inputtxt.value.match(Upper)) {
        alert('uppercase only...')
        return true;
    } else if (inputtxt.value.match(number)) { 
        alert('numeric only...')
        return true;
    }
    else { 
        alert('Wrong...!')
        return false;
    }
} 

hide preview

Basically I validate account number starting with uppercase character and second character is numeric.

function CheckDecimal(inputtxt) 
{ 
    var Upper  = /^[A-Z]/; 
    var number = /^[0-9]/;
    if (inputtxt.value.match(Upper)) {
        alert('uppercase only...')
        return true;
    } else if (inputtxt.value.match(number)) { 
        alert('numeric only...')
        return true;
    }
    else { 
        alert('Wrong...!')
        return false;
    }
} 

Upvotes: 0

Views: 140

Answers (2)

Sam
Sam

Reputation: 632

In your case you're checking for the first character as number, while checking for number pattern. Instead you need to provide var number=/^[A-Z][0-9]/; as the number should be the second one.

Or you could use javascript to check the first and second character

if (!inputtxt) {
  return;
}
const firstChar = inputtxt.charAt(0);

if (firstChar !== firstChar.toUpperCase()) {
  alert("first character should be uppercase");
}

if (inputtxt.length > 1) {
  const secondChar = inputtxt.charAt(1);

  if (isNaN(secondChar)) {
    alert("second character should be numeric");
  }
}

Upvotes: 0

Heartbit
Heartbit

Reputation: 1836

You can use this pattern:

let pattern = /^[A-Z]+[0-9]+([a-z0-9A-Z]{1,})?/

Upvotes: 1

Related Questions