简体   繁体   English

如何在angularjs中验证仅具有特殊字符的正则表达式?

[英]How to validate regular expression having only special character in angularjs?

I have to do validation for particular text value. 我必须对特定的文本值进行验证。 If string is empty, i have to return as please enter some value. 如果字符串为空,我必须返回,请输入一些值。 The text field should allow alphanumeric and special characters .@_ .If user enter any other special character, need to return special characters except .@_ not allowed. 文本字段应允许使用字母数字和特殊字符。@ _。如果用户输入任何其他特殊字符,则需要返回特殊字符,但不允许使用。@ _。 And if they enter only special characters, i have to return 'do not enter special char'? 如果他们仅输入特殊字符,我必须返回“不输入特殊字符”? I tried below method, but i did not work out. 我尝试了以下方法,但没有解决。 Any help pls? 有什么帮助吗?

Please see updated code here. 请在此处查看更新的代码。

const test1 = '[^A-Za-z0-9_||@||\. ]';
const test2 = '[A-Za-z0-9]';
const whitespace = '/^\s*$/';
const onlyspecial = '/[.@_ ]/';

if(strval.match(whitespace))
{
    return "Please enter value";
}
if(strval.match(test1))
{
    if(strval.match(test2)|| strval.match(onlyspecial))
    {
        return "special character except @,space,underscore and . not allowed";
    }
    else
    {
        return "do not enter only special char";
    }
}

Results for various input:

strval = "abcd@123" - pass
strval = "abcd_123" - pass
strval= "abcd test" - pass
strval = "abcd.test - pass
strval = .@_   - do not enter only special char (though it is acceptable characters, only entering these should not accept)
strval = "abcd$%^" - special character except @,space,underscore and . not allowed
strval = "abcs2323&&" - special character except @,space,underscore and . not allowed
strval = "$$$%%^&&"  - do not enter only special char
strval = ".@_1234"  - please enter valid name

Replace console.log(...) with return it should work. 将return。替换为console.log(...),它应该可以工作。

var strVal = 'Abcd';

const onlySpecial = /[@_.]/
const alphaNumeric = /[A-Za-z0-9]/
const alphaNumericSpecial = /[^A-Za-z0-9@_.]/

if (strVal == '') {
    console.log("Nop")
} else if (alphaNumericSpecial.test(strVal)) {
    console.log("Only Alphanumeric and @, _, .")
}

else {
    if (onlySpecial.test(strVal) && alphaNumeric.test(strVal)) {
    console.log("Good Character.")
    } else if (onlySpecial.test(strVal) && !alphaNumeric.test(strVal)) {
    console.log("Don't Enter only special character.")
    } else {
    console.log("Alphanumeric..")
    }
}
     const test1 = '[^A-Za-z0-9 \S||@||\.||_]';
     const test2 = '[A-Za-z0-9]';
     const whiteSpace = /^\s*$/;


      if (strValue.match(whiteSpace)){
           return "Please enter value";
      }else if (strValue.match(test1)) {
           return "Please enter valid name (except .@_ )";
      }else if (!strValue.match(test2)){
           return "Don't enter only special characters";
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写正则表达式以验证特殊位置的字符? - How to write regular expression to validate character in special places? 如何使用正则表达式验证密码是否允许至少4位数字和1个字符而没有特殊字符? - How to validate a password allows at least 4 digits and 1 character without special character using regular expression? 具有特殊字符的angularjs表达式 - angularjs expression with special character 正则表达式不适用于特殊字符 - Regular expression not working for special character 特殊字符密码的正则表达式 - regular expression for password for special character 如何使用jQuery正则表达式验证字符,数字和特殊字符? - How to validate characters, numbers and special characters using jQuery regular expression? 如何在正则表达式中启用向密码验证添加特殊字符 - How to enable adding special character to the password validation in regular expression 寻找正则表达式以验证密码,长度必须大于8个字符,并且至少包含一个特殊字符 - looking for a regular expression to validate a password, must be greater than 8 characters long and contain at least one special character 正则表达式以验证特殊字符集 - Regular expression to validate set of special characters 在Javascript中使用正则表达式匹配特殊字符“-” - Matching Special character '-' using Regular expression in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM