最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validating Alpha-Numeric values with all Special Characters - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked May 4, 2012 at 21:22 AbbasAbbas 5,04431 gold badges99 silver badges165 bronze badges 1
  • my bad, didn't read the question correctly. Do you want different filters for alpha-numeric and non-alpha-numeric characters? – Fabrício Matté Commented May 4, 2012 at 21:31
Add a comment  | 

5 Answers 5

Reset to default 7

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 \.

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

You can try this for all special characters

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

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"];

If want to allow specific special characters with alpha numeric then following regexp will work.You can customize, allowed special characters range as per your requirement.In case of escape characters you need to put in between \ \. In below example \-\ allows '-'.

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

Hope this will help you :).

发布评论

评论列表(0)

  1. 暂无评论