Reputation: 77
I want to create a custom validator that check if the input is a string and not a number. Example: I enter 2 in the input it should display the error message and it will be triggered when I put data-parsley-name
<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script type="text/javascript" src="js/parsley.min.js"></script>
<input type="text" id="f_name" name="f_name" placeholder="Enter First Name" data-parsley-required="true" data-parsley-trigger=" focusout" data-parsley-name>
window.Parsley.addValidator('name', {
requirementType: 'string',
validateString: function(value) {
if(isNaN(value)){
return value;
}
},
messages: {
en: 'Enter a Valid Name',
}
});
Upvotes: 3
Views: 612
Reputation: 3335
In your validator function change this:
validateString: function(value) {
if(value === "" + value){
return value;
console.log(this.messages.en);
}
}
But if you want to validate numbers use this code: if(Number(value) === value);
.
Number(value)
is turning your value to the number. And if it is equal to your input it will return it and print the message.
Upvotes: 0
Reputation: 10857
Use jQuery's .isNumeric()
https://api.jquery.com/jQuery.isNumeric/
validateString: function(value) {
if($.isNumeric(value) == false){
return value;
}
}
Upvotes: 0