Andi Crew
Andi Crew

Reputation: 129

How to use if in var field check validate?

I have some code for input validate:

var ex_info = {
   email: {type: "email", config: {require: true}},
    amount_from: {type: "text", config: {require: true, is_chars: ",.[:num]"}},
    wallet_from: {type: "text", config: {require: true}},
    phone: {type: "text", config: {require: false, is_chars: "+[:num]", min: 6}},
    name: {type: "text", config: {require: true}},
    fname: {type: "text", config: {require: true}}
};

How to use if(){} else {} for that field wallet_from: {type: "text", config: {require: true}}, ? I want to switch required to true or false by condition.

Upvotes: 0

Views: 47

Answers (3)

Matt
Matt

Reputation: 324

You could use ternary (replace 'conditional statement' with whatever you are testing for): ex_info.wallet_from.config.require = conditional statement ? true : false;

Upvotes: 0

bla
bla

Reputation: 321

You can use a conditional operator.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

ex_info.wallet_from.config.require = myCondition === isTrue ? true : false

Upvotes: 1

mdatsev
mdatsev

Reputation: 3879

Just do

ex_info.wallet_from.config.require = condition

Or if condition is not a boolean:

ex_info.wallet_from.config.require = !!condition

Upvotes: 1

Related Questions