JavaScript -仅验证数字/字母

JavaScript - validating for only numeric/alphabet

本文关键字:字母 数字 验证 JavaScript      更新时间:2023-09-26

我知道有很多类似的主题线,所以我将直接澄清我的问题。有一个简单的形式,我正在验证,其中测试的空字段运行完美,并遵循各种其他论坛帖子后,已经尝试实现测试的数字或字母输入,但功能根本不会起作用。

下面是NotEmpty函数的一个片段:
 function validateNotEmpty( input, id )
     {
        var errorDisplay =  document.getElementById( id );
        var notEmpty;
        if ( input == "" ) // check if input is empty
        {
           errorDisplay.innerHTML = "This is a requiered field";
           notEmpty = false; // update return value
        } // end if
        else
        {
           errorDisplay.innerHTML = ""; // clear the error area
           notEmpty = true; // update return value
        } // end else
        return notEmpty; // return whether the input is empty
     }// end function validateNotEmpty

以下是我的两个(不工作的数字/字母测试器):

function validateNotNumber(input, id) 
    {
            var errorDisplay =  document.getElementById( id );
            var notNumber;
            for (var i=0;i<input.value.length;i++){
            temp=input.value.substring(i,i+1);
                console.log(temp);
            if (digits.indexOf(temp)==-1){
                errorDisplay.innerHTML = "Sorry, this section can not have numbers";
                notNumber =  false;
               }
            else
             {
               errorDisplay.innerHTML = ""; // clear the error area
               notEmpty = true; // update return value
             } // end else
         }
         return notNumber;
    }

function validateNotLetter(input, id) 
            {
                var errorDisplay =  document.getElementById( id );
                var notLetter;
                for (var i=0;i<input.value.length;i++){
                temp=input.value.substring(i,i+1);
                    console.log(temp);
                if (alphabets.indexOf(temp)==-1){
                    errorDisplay.innerHTML = "Sorry, this section can not have letter";
                    notLetter =  false;
                   }
                else
                 {
                   errorDisplay.innerHTML = ""; // clear the error area
                   notLetter = true; // update return value
                 } // end else
             }
             return notLetter;
        }

在此之上,这就是我如何从我的表单调用javascript函数:

<td width="135" valign="top">
                          <input size="15" type="text" name="Full_Name"  value="" onblur = "validateNotNumber( this.value, 'nameError' )"/>
                         <p> <span id = "nameError" class = "errorMessage"></span> </p>
</td>

谢谢你的帮助/指点,我对JavaScript还是很陌生的,所以我的头围绕着验证已经相当复杂了

小提琴帮你开始。

我更新了它,基本上做你想做的,但使用正则表达式来检测数字,字母等。

其中一个的样本:

 function validateNotLetter(input, id) {
   var errorDisplay = document.getElementById(id);
   var value = input || '';
   var letterRegex  = /'D+/i;
    var hasALetter = letterRegex.test(value);
   if(hasALetter){
     errorDisplay.innerHTML = "Sorry, this section can not have letter";;
   }else{
    errorDisplay.innerHTML = "";
   }
   return notLetter;
 }

正则表达式是强大的。在线资源:

  1. https://regex101.com/-测试正则表达式

  2. 的地方
  3. https://regexone.com/-学习的地方

这种方式太复杂了。你绝对应该尝试一些正则表达式,例如

    var alphanumeric = "someStringHere";
    var myRegEx  = /[^a-z'd]/i;
    var isValid = !(myRegEx.test(alphanumeric));
    console.log(isValid);

或函数:

function isValid(str) {
  var myRegEx  = /[^a-z'd]/i;
  return !(myRegEx.test(str));
}