【问题标题】:Check first character of a form field is a letter检查表单域的第一个字符是字母
【发布时间】:2014-11-23 19:13:21
【问题描述】:

我需要能够检查表单域中的第一个条目是否是字符。 类似于下面的东西(但用'magic'替换'somethinginhere'。 在此先感谢

<script type="text/javaScript">
function Validator() {
    if (document.theForm.Code.value!= "<somethinginhere>") {
        alert("Code needs to start with a letter");
    }
}
</script>

【问题讨论】:

标签: javascript forms validation field


【解决方案1】:

您可以像这样使用正则表达式:

function Validator(astring) {
  return  /^[a-zA-Z]/.test(astring);
}

【讨论】:

    【解决方案2】:
        <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2013-11-18
      相关资源
      最近更新 更多