pragya.go
pragya.go

Reputation: 169

Bootstrap email validation regular expression

The following code accepts admin@admin. How can I make it invalid. It should accept [email protected]. So basically I need the domain name to make the value accepted.

fields: {
          txtEmail: {
            validators: {
               emailAddress: {
                  message: 'The email address is not valid'
               }
            }
          } 
        }

Upvotes: 0

Views: 2626

Answers (3)

Thien Hoang
Thien Hoang

Reputation: 1

 fields: {
        email: {
            validators: {
                notEmpty: {
                    message: 'Địa chỉ email không được để trống.'
                },
                
                regexp: {
                    regexp: /^[\w,\\"]+([-+.\']\w+)*@(?:(?=[a-z,A-Z])\w+([-.]\w+)*\.\w+([-.]\w+)*$|(?![a-z,A-Z])((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)/,
                    message: 'This value not valid.'
                }
                
                
            }
        },

and in html remove type="email" in input

<label for="email">Email<span style="color: red;"> &#42;</span></label>
<input id="email" class="form-control form-control-lg font-weight-600"  name="email"  placeholder="Địa chỉ email" required=""> 

Upvotes: 0

Nikunj Kathrotiya
Nikunj Kathrotiya

Reputation: 963

emailAddress: {
    regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$',
    message: 'The value is not a valid email address'
}

Upvotes: 0

Danish
Danish

Reputation: 1497

I always use this function, proved to be best so far for me

/*
*   Validate Email
*   @params element
*   @return boolean false || true
*/
var validateEmail  = function(element){
    var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
    if(   !email_regex.test( element )   ) {
        return false;
    } else{
        return true;
    }
};

now for using you can do something like this

validateEmail( $('#email_elem').val()  )

EDIT: Sorry for not posting answer related to BootstrapValidator

so for bootstrapvalidator do following,

 regexp: {
        regexp:  /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i,
        message: 'Please enter correct email
    }

could you try above regex? & see if it works. OR try this

fields: {
    email: {
        validators: {
            emailAddress: {
                message: 'The value is not a valid email address'
            },
            regexp: {
                regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$',
                message: 'The value is not a valid email address'
            }
        }
    }
}

EDIT: created this working fiddle please check https://jsfiddle.net/yeoman/436rvcut/3/

Upvotes: -1

Related Questions