0

I am writing a Javascript function to validate a user input. The condition is that, the data should have at least one number.

var userName = "abcde200"

function isNumeric(userName){
   userName = parseInt(userName);
   userName = !isNaN(userName);
   return userName;
}

The function returns "true" when the value starts with number like: "200abcde" but doesn't return "true" value with if the numbers are between alpha or at the end. I know, this could be done easily with regular expression but I want to achieve this without regular expression.

Note: There are plenty of similar questions here but didn't work any of them to solve my case.

Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110

1 Answers1

1

If you don't want to use regular expressions, you can split your string into an array of characters and then check the characters to see if it at least one of them are a number by using the some() method.

function isNumeric(userName) { 
  return userName.split('').some(function(c) {
    return !isNaN(c); 
  });
}

Like other people have said, using a regular expression would be a much cleaner way of doing it.

Saad
  • 49,729
  • 21
  • 73
  • 112