javascript 特殊字符验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16667329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:34:04  来源:igfitidea点击:

Special character validation

javascriptfunctionvalidation

提问by AJJ

I have some javascript written to validate that a string is alphanumeric but i was just wondering how i could add some code to include hyphens(-) and slash's(/) as acceptable inputs. Here is my current code:

我编写了一些 javascript 来验证字符串是否为字母数字,但我只是想知道如何添加一些代码以将连字符 (-) 和斜杠 (/) 作为可接受的输入包含在内。这是我当前的代码:

function validateAddress() {
  var address = document.getElementById('address');

  if (address.value == "") {
    alert("Address must be filled out");
    return false;
  } else if (document.getElementById('address').value.length > 150) {
    alert("Address cannot be more than 150 characters");
    return false;
  } else if (/[^a-zA-Z0-9\-\/]/.test(address)) {
    alert('Address can only contain alphanumeric characters, hyphens(-) and back slashs(\)');
    return false;
  }
}

回答by Barney

Simply add them to the character group. Of course, because both -and /are special characters in this context (/ends a RegExp, -expresses a range), you'll need to escape them with a preceding \:

只需将它们添加到字符组即可。当然,因为在此上下文中-/都是特殊字符(/结束正则表达式,-表示范围),您需要使用前面的 将它们转义\

function validateAddress(){
    var TCode = document.getElementById('address').value;

    if( /[^a-zA-Z0-9\-\/]/.test( TCode ) ) {
        alert('Input is not alphanumeric');
        return false;
    }

    return true;     
}

回答by ani

function isValidCharacter(txtTitle) {   
     var title = document.getElementById(txtTitle);
     var regExp = /^[a-zA-Z]*$/
     if (!regExp.test(title.value)) {
        title.value = '';
        return false;
        }
      else {      
           return true;
        }
   }


function Validation(){
 var txtTitles = document.getElementById('txtTitle');
  if (isValidCharacter(txtTitles.id) == false) {
   alert("Please enter valid title. No special character allowed.");        
    return false;
  }  
 }



   $("#Btn").unbind("click").click(function () {
        if (Validation() == false) {

        }
        else {
              //success     
        }
   }

回答by Ann

function namefun(c)
            {   
                var spch=/[A-z\s]/ig;
                var dig=/[0-9]/g;
                var ln=c.length;
                var j=1;
                for(var i=0;i<ln;i++)
                {
                    var k=c.slice(i,j);
                    if((spch.test(c)==false)||(dig.test(c)==true))
                    {
                        alert("Invalid name");
                        document.getElementById('tname').value="";
                        ln=0;
                        setTimeout(function(){tname.focus();}, 1);
                        //return false;
                    }
                    j++;
                }

            }