[英]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.