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 6683927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:52:45+00:00 2026-05-26T04:52:45+00:00

I’ve created a validation jQuery widget to validate form fields. I want to be

  • 0

I’ve created a validation jQuery widget to validate form fields. I want to be able to attach two (or more) validation rules some fields e.g.

$('#textField').validation({type: 'minLength', extraParameters: {length: '4',},}); 
$('#textField').validation({type: 'maxLength', extraParameters: {length: '20',},}); 

which sets both a minimum and maximum length rule on the field ‘textField’.

It seems that attaching multiple widgets with the same class makes the objects get overwritten. i.e. when creating the objects with the code below the page alerts with:
made a minLength
made a maxLength

but then when “Validation.validateAll();” is called the page alerts with:

Validating type is: maxLength
Validating type is: maxLength

i.e. the second object has overwritten the options of the first object.

Is it possible to make multiple instance of the same widget on the same object and I’m just doing it wrong?
Or is this not possible and I should use a different approach?

cheers
Dan

The code for the validation class is:

var Validation = {

pageValidations: [],

validateAll: function(){    
    for(var i=0; i<Validation.pageValidations.length ; i++){
        validation = Validation.pageValidations[i];
        validation.validate();
    }
},

setErrorMessage: function(errorString, parameters){
    var errorElementID = "" + this.element.context.name + "Error";
    $("#" + errorElementID).show();
    $("#" + errorElementID).text(errorString);
},

clearErrorMessage: function() {
    var errorElementID = "" + this.element.context.name + "Error";

    $("#" + errorElementID).hide();
},

validateMaxLength: function() { 
    try{
        var stringFromInput = this.element.context.value;
        var maxLength = this.options.extraParameters.length;

        if(stringFromInput.length > maxLength){ 
            this.setErrorMessage("Length of field is too long.");
            return false;
        }
    }
    catch(error){
        alert("Exception in validateMinLength, " + error);
    }
    return true;  
},

validateMinLength: function() {

    try{
        var stringFromInput = this.element.context.value;
        var minLength = this.options.extraParameters.length;

        if(stringFromInput.length < minLength){
            this.setErrorMessage("Length of field is too short.");
            return false;
        }
    }
   catch(error){
    alert("Exception in validateMinLength, " + error);
    }

    return true;  
},

validate: function (){

    var validationResult = false;

    alert("Validating type is: " + this.options.type);

    switch(this.options.type){
        case('minLength'):
            validationResult = this.validateMinLength();
        break;

        case('maxLength'):
            validationResult = this.validateMaxLength();
        break;
    }

    if(validationResult == false){
        this.options.isFailed = true;
    }

    return validationResult;
},

valueChanged: function (event){
    try{
        if(this.options.isFailed != false){
            var isValid = this.validate(); 

    if(isValid == true){
        this.clearErrorMessage();
    }
        }
    }
    catch(error){
       alert("Exception in valuechanged " + error);
    }
},

_create: function() {
        //alert("create called");
},

_init: function() {
    this.element.bind('change', jQuery.proxy(this, 'valueChanged'));

    $(this.element).addClass('validation');

    if(this.options.type == 'minLength'){
        alert("made a minLength ");
    }

    if(this.options.type == 'maxLength'){
        alert("made a maxLength ");
    }

    Validation.pageValidations.push(this);
},

options: {
    type: false,
    isFailed: false,
    extraParameters: {}
}
};

$.widget("dialogue.validation", Validation); // create the widget

function    setupValidationFields(){
    var object1 = $('#someMaxLength').validation({type: 'minLength', extraParameters: {length: '4',},}); 
var object2 = $('#someMaxLength').validation({type: 'maxLength', extraParameters: {length: '20',},}); 
  • 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-26T04:52:46+00:00Added an answer on May 26, 2026 at 4:52 am

    An alternative approach you could use is having your validation widget receive a list of rules in the initialiser, something like the following:

    $('#textfield').validation({
        rules: [
            {type: "minLength", …},
            {type: "maxLength", …}
        ]
    });
    

    That said, I think you should just use the excellent validation plugin that’s already there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.