Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:31:51+00:00 2026-05-27T20:31:51+00:00

I have been searching for a long time now on how to do the

  • 0

I have been searching for a long time now on how to do the simplest jQuery validate. (To change border color and background) That’s it, Nothing more! no text or anything extra. I have seen many ways but the code is just too long.

Here’s example of a long code. I dont think its effective way if i had more then 10 fields to validate.

$(document).ready(function(){
//global vars
var form = $("#customform");
var name = $("#name");
var email = $("#email");

//On blur
name.blur(validateName);
email.blur(validateEmail);

//On key press
name.keyup(validateName);

//On Submitting
form.submit(function(){
    if(validateName() & validateEmail())
        return true
    else
        return false;
});

//validation functions
function validateEmail(){
    //testing regular expression
    var a = $("#email").val();
    var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
    //if it's valid email
    if(filter.test(a)){
        email.removeClass("error");
        return true;
    }
    //if it's NOT valid
    else{
        email.addClass("error");
        return false;
    }
}
function validateName(){
    //if it's NOT valid
    if(name.val().length < 4){
        name.addClass("error");
        return false;
    }
    //if it's valid
    else{
        name.removeClass("error");
        return true;
    }
}
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:31:51+00:00Added an answer on May 27, 2026 at 8:31 pm

    It smells as if you need some refactoring and extract the code that gets repeated all the time into its own function:

    function validateElement(element, condition) {
        if(condition){
            element.removeClass("error");
        }
        else {
            element.addClass("error");
        }
        return condition;
    }
    

    Use it like this (I also recommend turning your variables into parameters so
    that you can use the validators on different fields):

    function validateEmail(field) {
        //testing regular expression
        var a = field.val(),
        filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
        return validateElement(field, filter.test(a));
    }
    
    validateEmail($('#email'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a long time now I have been searching through the forum whilst teaching
I have been searching the net for quite sometime now on how to extend
I have been searching the web for an answer now for quite a while,
I have been testing my geolocation query for some time now and I haven't
I have been told that searching in a MySQL database with LIKE '%wordend' is
I've been searching the web for a really long time, and I can't get
For a very long time now I've been using task queues on AppEngine to
Ok, so I have been searching for quite some time on this. I have
I have been searching for some kind of tool to analyse log files that
I have been searching around for a SQL function that tests a value for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.