-2

I need to be able to check if the first entry in a form field is a character. Something similar to below (but with the 'magic' replacing 'somethinginhere'. Thanks in advance

<script type="text/javaScript">
function Validator() {
    if (document.theForm.Code.value!= "<somethinginhere>") {
        alert("Code needs to start with a letter");
    }
}
</script>
Cooshti
  • 1
  • 1
  • You have something similar [here][1] [1]: http://stackoverflow.com/questions/9862761/how-to-check-if-character-is-a-letter-in-javascript – Zivko Sep 29 '14 at 11:49

2 Answers2

1

You can use a Regular Expression like this:

function Validator(astring) {
  return  /^[a-zA-Z]/.test(astring);
}
Fran Verona
  • 5,438
  • 6
  • 46
  • 85
Fabio
  • 1,890
  • 1
  • 15
  • 19
0
    <script type="text/javaScript">
    function Validator() {
    var letters = '/^[A-Za-z]+$/'; 
    var element=document.getElementById("myText").innerHTML;

    if (!element.match(letters)) {
        alert("Code needs to start with a letter");
    }
}
    </script>
SULFIKAR A N
  • 436
  • 4
  • 13