How can i validate the date field on frontend with magento default date validation class. I think it has one.
For required entry, I used data-validate:
<input type="text" placeholder="<?php /* @escapeNotVerified */ echo __('DOB (YYYY-MM-DD)') ?>" name="dob" id="dob" value="<?php echo $profileData['dob']; ?>" title="<?php /* @escapeNotVerified */ echo __('Date Of Birth') ?>" class="input-text validate-date" data-validate="{required:true}">
and added the form validate script:
<script type="text/javascript">
//added by navin
require(['jquery','mage/mage'], function(){
var dataForm = jQuery('#edit-form-validate');
dataForm.mage('validation', {});
});
</script>
required validation is working fine but I want to validate if the input date is in correct date format.
This also arise a question, In which file magento defines all the validation class?
asked Nov 21, 2016 at 10:34
Navin Bista
1,1035 gold badges14 silver badges31 bronze badges
1 Answer 1
You can do it using errorPlacement function,
<script type="text/javascript">
//added by navin
require(['jquery','mage/mage'], function(){
var dataForm = jQuery('#edit-form-validate');
dataForm.mage('validation', {
errorPlacement: function(error, element) {
var errorPlacement = element;
console.log(element.val());
if (element.val()) {
//do your custom validation code.....
}
}
});
});
</script>
answered Nov 21, 2016 at 10:53
Rakesh Jesadiya
42.5k19 gold badges135 silver badges186 bronze badges
-
ok I got your point.But i was searching for some class that validates date, I think there should be one don't you think so? like validate-date.Navin Bista– Navin Bista2016年11月21日 11:22:09 +00:00Commented Nov 21, 2016 at 11:22
-
1yes validate-date is used for date, you can check it from /lib/web/mage/validation.js folder.Rakesh Jesadiya– Rakesh Jesadiya2016年11月21日 11:24:24 +00:00Commented Nov 21, 2016 at 11:24
-
1All validation of magento frontend js is comes from validation.js file.Rakesh Jesadiya– Rakesh Jesadiya2016年11月21日 11:25:03 +00:00Commented Nov 21, 2016 at 11:25
-
so how do I use it here. I added the class vadidate-date but it is not working. should i add anything else?Navin Bista– Navin Bista2016年11月21日 11:33:18 +00:00Commented Nov 21, 2016 at 11:33
-
You have added correct class in your input. This is default class.Rakesh Jesadiya– Rakesh Jesadiya2016年11月21日 12:03:30 +00:00Commented Nov 21, 2016 at 12:03
default