Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Alpha-Numeric values with all Special Characters

i want to validate my text field with below:
1. alpha-numeric
2. And all special characters
i am not good in regex can anyone help me out creating a regex for above things.

like image 942
Abbas Avatar asked May 04 '12 21:05

Abbas


People also ask

Does alpha numeric include special characters?

Alphanumeric Characters and Alphanumeric Password Requirements. An alphanumeric password contains numbers, letters, and special characters (like an ampersand or hashtag).

What is alpha numeric special character?

Alphanumeric, also referred to as alphameric, is a term that encompasses all of the letters and numerals in a given language set. In layouts designed for English language users, alphanumeric characters are those comprised of the combined set of the 26 alphabetic characters, A to Z, and the 10 Arabic numerals, 0 to 9.

Can you easily check if all characters in the given string is alphanumeric?

The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).


4 Answers

alphanumeric Strings are matched like this:

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

It matches any string that only contains of the listed chars and is at least one char long.

With special chars it would work the same way.

But what do you consider to be a special char?

For !@#$%^&*()+=-[]\';,./{}|":<>? – being the set of special chars, the regex would look like this:

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

Again, all the allowed characters are listed. The ones used within regexes as commands or quantifiers have to be escaped with a \.

like image 157
Stefan Dochow Avatar answered Nov 15 '22 05:11

Stefan Dochow


This will do what you want.

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!');
}

Note that both regexes work on double-negation, returning false in the first match of an illegal character for best performance. First is the negation of the \W charset which is the negation of \w. Second is a negation ! of the negation ^ of the pre-defined characters (\w + pre-defined chars). Reply if you want any explanation or modifications.

EDIT Here's a regex to match if the string has at least one special character and alpha-numeric characters.

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

Is it ok or you need it in a single regex pattern?

EDIT This will do it in a single regex:

if (val.match(/(?=.*[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]+?).*[^_\W]+?.*/)
    alert('String contains both alpha-numeric and your pre-defined special characters!');
like image 22
Fabrício Matté Avatar answered Nov 15 '22 06:11

Fabrício Matté


You can try this for all special characters

 /^[0-9a-zA-Z\s\r\n@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?]+$/;
like image 28
Mukesh Avatar answered Nov 15 '22 05:11

Mukesh


For the alphanumeric and all special characters Validation

you can use in one regex

This will return true if you have Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character

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

        return [alphaNumberTest evaluateWithObject:@"yourString"];

Minimum 8 characters at least 1 Alphabet and 1 Number:

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

            return [alphaNumberTest evaluateWithObject:@"yourString"];

Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character:

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

                return [alphaNumberTest evaluateWithObject:@"yourString"];

Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number:

 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 Uppercase 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"];
like image 33
Arbaz Shaikh Avatar answered Nov 15 '22 07:11

Arbaz Shaikh