Semantic-ui: Custom form validation rules

Created on 24 Jan 2015  路  11Comments  路  Source: Semantic-Org/Semantic-UI

Is there any way to call a direct function to make validation rules?

E.g.

username: {
identifier : 'username',
rules: [
{
type: function(text){
(algorithm ... )
},
prompt : 'Please enter a username'
}
]
}

I don't see anywhere on docs, and something like that would be great!

Docs Issue

Most helpful comment

You can specify custom form validation rules by adding rules to $.fn.form.settings.rules then referencing the name in your init

$.fn.form.settings.rules.myrule = function(value) {
  return true; // always validate
}

All 11 comments

You can specify custom form validation rules by adding rules to $.fn.form.settings.rules then referencing the name in your init

$.fn.form.settings.rules.myrule = function(value) {
  return true; // always validate
}

I'll add this to docs.

Great, thanks, the docs specify

"Validation rules are found in $.fn.form.settings.rules, to add new global validation rules, modify $.fn.form.settings.rules to include your function."

But an example like you gave would be helpful!

There are use cases where you wouldn't want to put a rule in $.fn.form.settings.rules for one special field.
I think it would make sense if type is a function and it is run similarly to how a global rule runs? Would make it much more easy to create one-off rules.

Maybe not "type", because you are no longer specifying a type of validation, but a "validate" param or "fn" param that is given a function.

@jlukic will you consider adding such api? it does sound a bit too messy to put specific form validation rules in the "global scope" of $.fn.form.settings.rules.

+1 for inline function option

+1 for inline function option

+1 for inline function option

+1 for inline function option
this would allow for i18int date-time validation using moment.js and endless other cases

+ditto
handy for pre-filtering before passing to existing validators, e.g.

function (cardNumber, cardTypes) {
  return $.fn.form.settings.rules.creditCard(cardNumber.replace(/ +/g, ''), cardTypes);
}

to allow people to enter credit card numbers with spaces so they can scan/correct them more easily.

also for prototyping and testing hypotheses before formalising them as part of $.fn.form.settings.rules

+10000 for inline rules

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mllamazares picture mllamazares  路  3Comments

vinhtq picture vinhtq  路  3Comments

ghost picture ghost  路  3Comments

miguelmota picture miguelmota  路  3Comments

batata004 picture batata004  路  3Comments