Validating a purchase order number format - xmpie-users/uStore-js GitHub Wiki
If you use Purchase Order clearing type, you may want to validate the format of the PO number entered by the customer.
This script provides some examples of how that could be done.
You can adjust the script to handle different requirements using basic JavaScript techniques.
$(document).ready(function () {
//if the PO input is on the page
if ($("#ctl00_cphMainContent_ctlClearingUserData10001_txtPurchaseOrder").length) {
//add an event listner to check for changes to the PO input
$("#ctl00_cphMainContent_ctlClearingUserData10001_txtPurchaseOrder").change(function() {
//EXAMPLE 1 - if the PO is empty
//if ($(this).val().trim() == ""){
// $("#ctl00_cphMainContentFooter_btnCheckout").hide();
// $("#POmessage").remove();
// $(this).after("<span id='POmessage' style='color:red'> A value is required here.</span>");
//} else {
// $("#ctl00_cphMainContentFooter_btnCheckout").show();
// $("#POmessage").remove();
//}
//EXAMPLE 2 - if PO length is not 5 characters long
//if ($(this).val().trim().length != 5){
// $("#ctl00_cphMainContentFooter_btnCheckout").hide();
// $("#POmessage").remove();
// $(this).after("<span id='POmessage' style='color:red'> PO must be 5 characters long.</span>");
//} else {
// $("#ctl00_cphMainContentFooter_btnCheckout").show();
// $("#POmessage").remove();
//}
//EXAMPLE 3 - PO must start with "PO" or "PR" and then have 5 digits
var regex = /^(PO|PR)\d{5}/g;
if (!regex.test($(this).val())){
$("#ctl00_cphMainContentFooter_btnCheckout").hide();
$("#POmessage").remove();
$(this).after("<span id='POmessage' style='color:red'> Value must be like PO##### or PR#####.</span>");
} else {
$("#ctl00_cphMainContentFooter_btnCheckout").show();
$("#POmessage").remove();
}
});
//trigger the change automatically for the first loading of the page
$("#ctl00_cphMainContent_ctlClearingUserData10001_txtPurchaseOrder").change();
}
});