Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jquery form validation regular expression

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #34672
    stian
    Member

    The following checks if anything at all has been entered.


    $('#contact-form #submit').click(function () {

    //Get the data from all the fields
    var name = $('input[name=name]');
    var email = $('input[name=email]');
    var website = $('input[name=url]');
    var message = $('textarea[name=message]');

    //Simple validation to make sure user entered something
    //If error found, add hightlight class to the text field
    if (name.val()=='') {
    name.addClass('hightlight');
    return false;
    } else name.removeClass('hightlight');

    if (email.val()=='') {
    email.addClass('hightlight');
    return false;
    } else email.removeClass('hightlight');



    if (message.val()=='') {
    message.addClass('hightlight');
    return false;
    } else message.removeClass('hightlight');


    });

    How can I use a regular expression to check for a valid email?

    #88807
    Doppy
    Member

    You might wanna try RegExp


    var regExp = new RegExp();
    regExp = /^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z]*[-.w])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$/;

    if (regExp.test(email.val() == true) {
    //Do something here, when it is email
    } else {
    //Do other thing here.
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.