Search code examples
javascriptphpcodeigniterbootstrapvalidator

Work with regular expressions


i am using codeignter framework and i want to validate field using bootstrap validator.How i make a regular expression.I am usign bootstrap validator.

Example 1:I want to validate age field in which only allow numeric value.But i enter .(Dot or decimal) This is also consider true which is wrong. I want age like this:

Success Scenario Examples:
16  ,

  20 ,  50  , 60 
    Wrong Scenario Examples:
    16.5   ,  20.5 ,   25.5

    I want this will not consist of decimal point.
Here is my code:
professional_age: {
                validators: {
                    lessThan: {
                        value: 100,
                        inclusive: true,
                        message: 'The ages has to be less than or equal to 100'
                    },
                    greaterThan: {
                        value: 15,
                        inclusive: true,
                        message: 'The ages has to be greater than or equals to 15'
                    }
                }
            }

Example 2: I want email regular expression:

These are success scenario:
[email protected]

These are wrong scenario:
[email protected]
[email protected]
zain@gmail.
Please how we make regular expression:

Example 3:I want a regular expression.In which alphabet and numeric value allowed.One alphabet is must.

This is success scenario:
123456d
123d5456
a25d852e
This is wrong scenario:
11233658

Solution

  • Regex can be so:

    ^(?=.*[a-zA-Z])([a-zA-Z\d]+)$
    

    Positive Lookahead tests that at least one char is present

    demo and some explanation