Comment tester si une chaîne est exclusivement composée de caractères alphanumériques en JavaScript ?
La fonction suivante retourne vrai si la chaîne de caractères passée en paramètre est exclusivement composée de caractères alphanumériques (a-z ; A-Z ; 0-9) :
function isAlphanumeric(string)
{
for ( var i = 0; i < string.length; i++ )
{
ch = string.charAt(i);
if (!(ch >= '0' && ch <= '9') && // Numeric (0-9)
!(ch >= 'A' && ch <= 'Z') && // Upper alpha (A-Z)
!(ch >= 'a' && ch <= 'z')) // Lower alpha (a-z)
return false;
}
return true;
}
La fonction suivante retourne vrai si la chaîne de caractères passée en paramètre est exclusivement composée de caractères alphanumériques (a-z ; A-Z ; 0-9) :
function isAlphanumeric(string)
{
for ( var i = 0; i < string.length; i++ )
{
ch = string.charAt(i);
if (!(ch >= '0' && ch <= '9') && // Numeric (0-9)
!(ch >= 'A' && ch <= 'Z') && // Upper alpha (A-Z)
!(ch >= 'a' && ch <= 'z')) // Lower alpha (a-z)
return false;
}
return true;
}
| # | ID | Query | URL | Count |
|---|---|---|---|---|
| 0 | 7729 | Chaîne alphanumérique javascript | https://fr.ans.wiki/2568/comment-tester-si-une-chaine-est-exclusivement-composee-de-caracteres-alphanumeriques-en-javascript | 1 |