Validating alpha numeric values ​​with all special characters

I want to check my text box below:
1. alpha-numeric
2. And all the special characters
I'm not good at regex, can someone help me create a regex for the things above.

+5
source share
5 answers

Alphanumeric strings correspond as follows:

^[a-zA-Z0-9]+$

It matches any string that contains only the listed characters and has a length of at least one char.

With special characters, it will work the same way.

But what do you think is a special char?

! @# $% ^ & *() + = - []\';../{} | ": < > ? - , :

^[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]+$

, . , , \.

+6

, .

function validate()
{
        var val = <my string>;

        if (val == '')
            alert('String is empty!');

        else if (!val.match(/[_\W]/))
            alert('String contains only A-Z a-z 0-9 characters!');

        else if (!val.match(/^\w[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?]/))
            alert('String contains your predefined characters only!');
}

, , false . -, \W, \W. -, ! ^ (\W + ). , - .

, - .

if (val.match(/[^_\W]/) && val.match(/[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]/))
    alert('String contains both alpha-numeric and your pre-defined special characters!');

?

:

if (val.match(/(?=.*[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]+?).*[^_\W]+?.*/)
    alert('String contains both alpha-numeric and your pre-defined special characters!');
+3

 /^[0-9a-zA-Z\s\r\n@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?]+$/;
+1

, regexp. , . escape- \\. \-\allow '-'.

/^[a-zA-Z0-9?=.*!@#$%^&*_\-\s]+$/

, :).

0

-

true, 8 , , 1 , 1 , 1 1

NSString *alphaNumberandSpecialRegex =@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";
        NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];

        return [alphaNumberTest evaluateWithObject:@"yourString"];

8 1 1 :

NSString *alphaNumberandSpecialRegex =@""^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"";
            NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];

            return [alphaNumberTest evaluateWithObject:@"yourString"];

8 1 , 1 1 :

NSString *alphaNumberandSpecialRegex =@"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$";
                NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];

                return [alphaNumberTest evaluateWithObject:@"yourString"];

8 , , 1 , 1 1 :

 NSString *alphaNumberandSpecialRegex =@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$";
                    NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];

                    return [alphaNumberTest evaluateWithObject:@"yourString"];

Minimum 8 and Maximum 10 characters, at least 1 Aperture alphabet, 1 lowercase alphabet, 1 number and 1 special character:

NSString *alphaNumberandSpecialRegex =@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}";
                        NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];

                        return [alphaNumberTest evaluateWithObject:@"yourString"];
0
source

All Articles