Check for negative numbers in Jquery Validation plugin - jquery-validate

I am trying to set-up the jquery validation plugin and one of my inputs requires a number within the range of -121 and -123.
I have tried using the range() method :
$("#myform").validate({
rules: {
field: {
required: true,
range: [-121, -123]
}
}
});
However it doesn't allow any numbers to validate. I have tried using max/min as well but they too don't seem to work on negative numbers. Am I missing something?
Thanks

Does the smaller number need to be first? If so, you want range: [-123, -121] instead.

$.validator.addMethod('inputClassName',
function (value) {
return (Number(value) >= -123 && Number(value) <= -121);
}, 'Enter a number between -121 and -123');
You could add this custom validator method, just replace the inputClassName

Related

Free-jqGrid: Dynamically Setting Search input According to Custom Selected Operand

I'm using free jqgrid, I implemented custom search operands using "customSortOperations: {}", I want to change the search field input to a select when I choose one of my custom operands, is there a way to do that? if not is there an event that fires when the operand is chosen to listen for it and implement my custom search field change? here is a sample of how I'm using "customSortOperations: {}"
$.extend($.jgrid.defaults, {
customSortOperations: {
tst: {
operand: "T",
text: "Test",
}
},
});
You posted only short fragment of your code. Thus, I'm not sure that I full understand your problem. To use new custom search operand you should define filter method inside of customSortOperations.tst and include the operation "tst" inside of searchoptions.sopt of column definition in colModel. If you want to use the new operation "tst" as default searching operation then the operation "tst" should be the first in searchoptions.sopt array. See for example https://jsfiddle.net/OlegKi/f3gLde6m/2/ created for the old answer for more details.

How to set default value for Dry::Validation.Params scheme?

I have next scheme
Dry::Validation.Params do
optional(:per_page).filled(:int?, lteq?: 1000)
optional(:page).filled(:int?)
end
If I pass empty hash for validation I get empty output but I want to set default values for my data.
I tried Dry::Types.default but it does not add default values in output. That's what I tried.
Dry::Validation.Params do
optional(:per_page).filled(Dry::Types['strict.integer'].default(10), lteq?: 1000)
optional(:page).filled(:int?)
end
Is it possible to do what I want?
The Dry::Validation has not this purpose.
I recommend you to use dry-initializer on your params before pass it to the validation.
You can do something like this:
optional(:per_page).filled(Types::Integer.constructor { _1 || 10 })
Or define your own fallback strategy as here https://github.com/dry-rb/dry-types/pull/410
optional(:per_page).filled(Types::Integer.constructor { |input, type| type.(input) { 10 } })

How to show in CodeIgniter only the second parameter in form validation message?

E.g. I have this line in my language form_validation file:
$lang['min_length'] = "%s must be at least %s characters in length.";
And I want the output to be like this:
$lang['min_length'] = "This field must be at least %s characters in length.";
However, the problem is that it looks like this when echoed:
This field must be at least Your name characters in length.
which is wrong because it takes the first %s instead of the second %s.
How can I force CI to take the second %s? Is it possible?
Since the min_length is a standard function in the Form Validation Library and treated through this library, you can only do so by changing the core library.
But there is an easier way - using a callback function with the Form Validation Library:
$this->form_validation->set_rules('your_field', 'The Field label', 'callback_my_min_length[10]');
Then within your controller, add this:
public function username_check($str, $min_length)
{
if ( mb_strlen($str) >= $min_length)
{
return TRUE;
}
else
{
$this->form_validation->set_message('my_min_length', 'This field must be at least '.$min_length.' characters in length.');
return FALSE;
}
}
This is how I would do it.

How to check if a property constains a space in groovy?

I am new to grails, and I am having a problem on how to write the proper constraints of one of the properties of my class. I want to check if the input contains a space (' '). Here is my code..
static constraints = {
username nullable: false, blank: false, minSize: 6, matches: /[A-za-z0-9_]{6,}/, validator: {
Account.countByUsername(it) < 1
}
Please help me.
Thanks!
You would want to use a custom validator like:
username validator: { val -> if (val.contains(' ')) return 'value.hasASpace' }
Edit: As R. Valbuena pointed out, you would need to change your countByUsername() validator to a unique: true.
In addition to a custom validator, you can also use the matches validator to ensure that only valid characters are used.
It looks like you're using this in your original question and the regex you're using doesn't allow a space, so a username with a space should fail that validator.
If you want to give a special message to someone if they have a space in it (instead of some other invalid character), then doelleri's answer is the right way to do that.

Validation of phone numbers containing '+' and '-' characters

In CodeIgniter, how do i validate phone numbers containing '+' and '-' symbols?
You cannot enter a number with "-" since you defined integer as the validation rule. Therefore, the validation will fail. You need to work with RegEx so that you can create more complex validation. See the topic on validations in the CI manual for more info.
Your validation rule:
$this->form_validation->set_rules('foo', 'Number', 'callback_number_validation');
Note how the keyword callback_ is used to identify CI your function for validation.
Your callback function:
//$str will be the value you want to verify
function number_validation($str) {
return preg_match("your_regex", $str) ? true: false;
}

Resources