I am writing a code for a simple client-side form validation. I am trying to create object with properties & values used in a process of validation (for each separate input) and then attach them to the input nodes.
I am trying to find a way to pair these objects automatically to respective inputs. In the code below, I found a way to do this, but for a price to use eval() function, which I would rather avoid. Is there an other way to do this in JS?
<label class="c-contact-form__label" for="your_name">Your Name *</label>
<input class="" type="text" name="your_name" maxlength="50" size="30">
var your_name = {
regex: /^([a-z\u00C0-\u02AB,.'´`]{1,}\.?\s?)([a-z\u00C0-\u02AB,.'´`]?\.?\s?)+$/i,
required: true,
invalid: "Your name seems a little bit weird…",
empty: "Please, fill in your name."
};
// From within a function of eventListener:
var Merged = Object.assign(this, eval(this.name)); // Use of eval()
console.log(Merged.invalid); // "Your name seems a little bit weird…",
I was googling for a solution quite hard, and I've read the possible context[varName] solution, but it doesn't seem to work (or I dont't get it).