I added two new fields to the shipping step in checkout. For those fields, I trigger the validation function in the validateShippingInformation
function in shipping.js
.
Now for the default e-mail field, it has a validation on keyup. How would I also make my fields validate on keyup? The fields are added to email.html
.
Email.html
: (part of this file where I add the fields)
<fieldset class="fieldset create-account-fieldset" data-bind="fadeVisible: !isPasswordVisible()">
<div class="field">
<div class="control">
<input class="input-text"
type="checkbox"
name="create-account"
id="create-account-toggle" />
<label class="label" for="create-account-toggle">
<span data-bind="i18n: 'I would like to create an account'"></span>
</label>
<fieldset class="fieldset create-password-fieldset">
<div class="field">
<div class="control">
<div class="password-fields">
<input class="input-text"
data-bind="
css: {focused: this.textInput != ''},
event: {
keyup: validatePassword
}"
type="password"
data-password-min-length="6"
name="create-pw"
id="create-pw"
data-validate="{required:false, 'validate-customer-password':true}" autocomplete="off"/>
<label class="label" for="create-pw">
<span data-bind="i18n: 'Password'"></span>
</label>
</div>
</div>
</div>
<div class="field">
<div class="control">
<input class="input-text"
data-bind="
css: {focused: this.textInput != ''},
event: {
keyup: validatePassword
}"
type="password"
name="confirm-pw"
data-password-min-length="6"
id="confirm-pw"
data-validate="{required:false, equalTo:'#create-pw'}" autocomplete="off"/>
<label class="label" for="confirm-pw">
<span data-bind="i18n: 'Confirm password'"></span>
</label>
</div>
</div>
</fieldset>
</div>
</div>
</fieldset>
shipping.js
: (part where I added the validation for the fields)
/**
* @return {Boolean}
*/
validateShippingInformation: function () {
var shippingAddress,
addressData,
loginFormSelector = 'form[data-role=email-with-possible-login]',
emailValidationResult = customer.isLoggedIn(),
field,
createAccount,
createPassword,
confirmPassword;
if (!customer.isLoggedIn()) {
$(loginFormSelector).validation();
emailValidationResult = Boolean($(loginFormSelector + ' input[name=username]').valid());
createAccount = Boolean($('[name="newsletter-subscribe"]').attr('checked'));
if ($('[name="create-pw"]').val()) {
createPassword = $('[name="create-pw"]').valid();
}
if ($('[name="confirm-pw"]').val()) {
confirmPassword = $('[name="confirm-pw"]').valid();
createPassword = $('[name="create-pw"]').valid();
}
}
if (this.isFormInline) {
this.source.set('params.invalid', false);
this.triggerShippingDataValidateEvent();
if (emailValidationResult &&
this.source.get('params.invalid')
) {
this.focusInvalid();
return false;
}
shippingAddress = quote.shippingAddress();
addressData = addressConverter.formAddressDataToQuoteAddress(
this.source.get('shippingAddress')
);
//Copy form data to quote shipping address object
for (field in addressData) {
if (addressData.hasOwnProperty(field) && //eslint-disable-line max-depth
shippingAddress.hasOwnProperty(field) &&
typeof addressData[field] != 'function' &&
_.isEqual(shippingAddress[field], addressData[field])
) {
shippingAddress[field] = addressData[field];
} else if (typeof addressData[field] != 'function' &&
!_.isEqual(shippingAddress[field], addressData[field])) {
shippingAddress = addressData;
break;
}
}
if (customer.isLoggedIn()) {
shippingAddress['save_in_address_book'] = 1;
}
selectShippingAddress(shippingAddress);
}
if (!emailValidationResult) {
$(loginFormSelector + ' input[name=username]').focus();
return false;
}
return true;
}