Is there any way to check if the first character in the field is an alpha char in Jquery?
Asked
Active
Viewed 744 times
-3
-
Either use [regex](http://stackoverflow.com/questions/6067592/regular-expression-to-match-only-alphabetic-characters) or use [charCodeAt](https://www.w3schools.com/jsref/jsref_charcodeat.asp) and check if the value is in the range you want. – jrook Apr 13 '17 at 17:00
-
Yes, there is. Have you done *any* research? - Possible duplicate of [Best way to alphanumeric check in Javascript](http://stackoverflow.com/questions/4434076/best-way-to-alphanumeric-check-in-javascript) – Tyler Roper Apr 13 '17 at 17:00
-
Need to do some research - this is a basic javascript, meaning I would imagine there's 432,544 posts on it. – Rob Scott Apr 13 '17 at 17:01
-
Possible duplicate of [Best way to alphanumeric check in Javascript](http://stackoverflow.com/questions/4434076/best-way-to-alphanumeric-check-in-javascript) – Makyen Apr 16 '17 at 23:29
1 Answers
1
The answer is yes, of course...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Alpha</title>
</head>
<body>
<input type="text" id="alpha">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
var module = {
regex: /[a-zA-Z]/,
str: '',
init: function () {
module.listeners();
},
listeners: function () {
$('#alpha').on('input', module.alpha);
},
alpha: function () {
module.str = $('#alpha').val();
if (module.regex.test(module.str.charAt(0))) {
console.log('OK');
} else {
console.log('KO');
}
}
};
$(document).ready(module.init);
</script>
</body>
</html>

Badacadabra
- 8,043
- 7
- 28
- 49