PageSourceSearch

https://powertoswitch.co.uk/wp-content/plugins/powertoswitch//assets/js/validation.js?ver=1.0.0

js powertoswitch.co.uk collected 2026-09-30 16:35:27 UTC 5,743 bytes, 146 lines download raw bytes

1document.addEventListener('DOMContentLoaded', function($) {
2	var delayTimer;
3	// Setting fields to only allow numerical values (even if they're text fields)
4	const numericOnlyFields = document.querySelectorAll('.number-field input');
5
6	if (numericOnlyFields.length > 0) {
7		numericOnlyFields.forEach(function(field) {
8			field.addEventListener('input', function() {
9				field.value = field.value.replace(/[^0-9]/g, '');
10			});
11		});
12	}
13	
14	// Setting a maximum date on the date picker fields to be 18 years or older
15	if (typeof acf !== 'undefined') {
16		acf.add_filter('date_picker_args', function(args, $field) {
17			if ($field.hasClass('dob-field')) {
18					const currentDate = new Date();
19					const minYear = currentDate.getFullYear() - 18;
20					const minMonth = currentDate.getMonth();
21					const minDay = currentDate.getDate();
22					args.maxDate = new Date(minYear, minMonth, minDay);
23			}
24			if ($field.hasClass('joint-acc-dob')) {
25					const currentDate = new Date();
26					const minYear = currentDate.getFullYear();
27					const minMonth = currentDate.getMonth();
28					const minDay = currentDate.getDate();
29					args.maxDate = new Date(minYear, minMonth, minDay);
30			}
31			return args;
32		});
33	}
34	
35	//Moving the description based on the class ".block-message"
36	const blockMessages = document.querySelectorAll('.block-message');
37	
38	if (blockMessages.length > 0) {
39			blockMessages.forEach(function(blockMessage) {
40					const description = blockMessage.querySelector('.acf-label .description');
41					const acfInput = blockMessage.querySelector('.acf-input');
42	
43					if (description && acfInput) {
44							// Remove the description element from its current parent
45							description.parentNode.removeChild(description);
46							// Insert the description element as the first child of the acf-input element
47							acfInput.insertBefore(description, acfInput.firstChild);
48					}
49			});
50	}
51	
52	//Validating MPRN for SSE API
53	jQuery("#mprn input, #postcode-mprn input").on('input', function($) {
54		if (jQuery('#new-supplier input').val() == 14356 || jQuery('#new-supplier input').val() == 156338) {
55			clearTimeout(delayTimer);
56			delayTimer = setTimeout(function() {
57				if (jQuery('#mprn input').val().length < 11) {
58						jQuery('.mprn-message, #check-mprn').hide();
59						jQuery('.mprn-message').empty();
60						jQuery('.mprn-message').text('Please enter a valid MPRN. ');
61						jQuery('.mprn-message, #check-mprn').show();
62				} else if(jQuery('#postcode-mprn input').val().length < 7) {
63					jQuery('.mprn-message, #check-mprn').hide();
64						jQuery('.mprn-message').empty();
65						jQuery('.mprn-message').text('Please enter a Postcode. ');
66						jQuery('.mprn-message, #check-mprn').show();
67				} else {
68						jQuery('.mprn-message').empty();
69						jQuery('#check-mprn').show();
70						jQuery('.loading-container').show();
71						jQuery('.hide-on-load').hide();
72						jQuery.ajax({
73								type: "POST",
74								url: "https://powertoswitch.co.uk/api/mprn_lookup_sse.php?MPRN=" + jQuery('#mprn input').val(),
75								dataType: "json",
76								timeout: 45000, // sets timeout to 3 seconds
77								error: function() {
78										jQuery('.hide-on-load').show();
79										jQuery('.loading-container').hide();
80										// jQuery('.mprn-message').text('Please enter a valid MPRN.');
81								},
82								success: function(data) {
83										jQuery('.loading-container').hide();
84										if (data.return.Errors == null) {
85												jQuery('.mprn-message').text('Good job');
86												jQuery('#address_line_1 input').val(data.return.StreetNumber + ' ' + data.return.StreetAddress);
87												jQuery('#county select').val(data.return.CountyName);
88												jQuery('#city input').val(data.return.TownCityName);
89												jQuery('#postcode-mprn input').val(data.return.PostCode);
90												jQuery('#meternumber input').val(data.return.MeterNumber)
91												var address_show = '' + data.return.StreetNumber.toString() + '<br>' + data.return.StreetAddress + '<br>' + data.return.CountyName + '<br>' + data.return.TownCityName + '<br>' + data.return.PostCode;
92												jQuery('.hide-on-load').hide();
93										} else if (data.return.ExistingCustomer == true) {
94												jQuery('.hide-on-load').show();
95										} else {
96												jQuery('.hide-on-load').show();
97										}
98								}
99						});
100				}
101			}, 1500);
102		}
103	});
104	
105	//Setting Min & Max of 5 Digits For Meter Read (only if field is not empty)
106	if(document.getElementById('acf-field_63ca7e2d47773-field_63f7815f3cbce-field_63f781a3cba52')){
107		document.getElementById('acf-field_63ca7e2d47773-field_63f7815f3cbce-field_63f781a3cba52').addEventListener('input', function(event) {
108			let value = event.target.value;
109			let container = document.querySelector('.acf-field-63f781a3cba52 .acf-input');
110			let errorMessage = container.querySelector('.acf-notice.-error.acf-error-message');
111			
112			if (value.length > 0) {
113				if (value.length !== 5 || isNaN(value)) {
114					event.target.setCustomValidity('Meter read must be 5 digits');
115					
116				if (!errorMessage) { // only append error message if it's not already displayed
117					let errorDiv = document.createElement('div');
118					errorDiv.className = 'acf-notice -error acf-error-message';
119					errorDiv.innerHTML = '<p>Meter read must be 5 digits</p>';
120					container.prependChild(errorDiv);
121				}
122			} else {
123				event.target.setCustomValidity('');
124				if (errorMessage) {
125					errorMessage.remove();
126				}
127			}
128		} else {
129			event.target.setCustomValidity('');
130			if (errorMessage) {
131				errorMessage.remove();
132			}
133		}
134	});
135	}
136	
137	if(jQuery('.payment_type.new-payment u').text() == 'Pay As You Go'){
138		jQuery('.payg').show();
139		jQuery('.direct_debit').hide();
140	}else{
141		jQuery('.payg').hide();
142		jQuery('.direct_debit').show();
143	}
144
145	
146});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.