jquery - How to check the string has number and special characters in JavaScript? - Stack Overflow

I have the below function to find the string contains numbers and special characters, but it was not wo

I have the below function to find the string contains numbers and special characters, but it was not working

let validateStr = (stringToValidate) => {
var pattern = /^[a-zA-Z]*$/;
if (stringToValidate&& stringToValidate.length > 2 && pattern.test(stringToValidate)) 
    {
    return false;
} else {
    return true;
}
};
validateStr("123$%^$") // I need the above function should return false.
validateStr("sdsdsdsd$%^$") // true
validateStr("sdsdsdsd") // true
validateStr("sdsdsdsd45678") // false
validateStr("$#*()%^$")//false
validateStr("123434333")//false

I have the below function to find the string contains numbers and special characters, but it was not working

let validateStr = (stringToValidate) => {
var pattern = /^[a-zA-Z]*$/;
if (stringToValidate&& stringToValidate.length > 2 && pattern.test(stringToValidate)) 
    {
    return false;
} else {
    return true;
}
};
validateStr("123$%^$") // I need the above function should return false.
validateStr("sdsdsdsd$%^$") // true
validateStr("sdsdsdsd") // true
validateStr("sdsdsdsd45678") // false
validateStr("$#*()%^$")//false
validateStr("123434333")//false
Share Improve this question edited Dec 27, 2018 at 13:04 parithi info asked Dec 26, 2018 at 11:07 parithi infoparithi info 2632 gold badges4 silver badges10 bronze badges 3
  • Possible duplicate of Special character validation – Najam Us Saqib Commented Dec 26, 2018 at 11:17
  • @Najamussaqib this is not duplicate, check the 4th function call, it was not working in your case .validateStr("$#*()%^$") check it with ur pattern. – parithi info Commented Dec 26, 2018 at 11:25
  • Your pattern is entirely optional so everything will match. – Álvaro González Commented Dec 26, 2018 at 11:49
Add a ment  | 

2 Answers 2

Reset to default 2

Your RegEx should be:

/[a-zA-Z]+[(@!#\$%\^\&*\)\(+=._-]{1,}/

Try the following way:

let validateStr = (stringToValidate) => {
  var pattern = /[a-zA-Z]+[(@!#\$%\^\&*\)\(+=._-]{1,}/;
  if ( stringToValidate && stringToValidate.length > 2 && pattern.test(stringToValidate)) {
    return true;
  } else {
    return false;
  }
};
console.log(validateStr("123$%^$"));      //false
console.log(validateStr("sdsdsdsd$%^$")); //true
console.log(validateStr("sdsdsdsd45678"));//false
console.log(validateStr("$#*()%^$"));     //false
console.log(validateStr("123434333"));    //false

You can use this code to track number and special characters in your string value.

 function checkNumberOrSpecialCharacters(stringValue){

    if( /[^a-zA-Z\-\/]/.test( stringValue) ) {
        alert('Input is not alphabetic');
        return false;
    }
 alert('Input is alphabetic');
    return true;     
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745352660a4623928.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信