Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
JavaScript API - Sync Method
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="JayC" data-source="post: 466020" data-attributes="member: 36373"><p>Yeah I am working on recoding this, I see what my issue is. </p><p></p><p>It is expecting you to use the 'success' method to invoke the behavior you are expecting. So I am going to make an async function to control my message to the display, and hope it works, aha.</p><p>[automerge]1607912205[/automerge]</p><p>Got it working. I recoded the system to use the 'Success' method as recommended by about every article I could find.</p><p></p><p>Here is the page now updating:</p><p></p><p>(First: The entry is 10 digits, and unique)</p><p>(Second: The entry is 10 digits, already taken)</p><p>(Third: The entry is 10 digits, available)</p><p></p><p>[ATTACH=full]11769[/ATTACH]</p><p></p><p></p><p>Thanks for the help! Here is what I ended up doing:</p><p></p><p></p><p>Added another anonymous function into my class for custom validation:</p><p></p><p>Here I tell it which index to use for the validation number, and whether it is invalid or valid</p><p>[CODE]addAsyncInvalidity: function(index, isInvalid){</p><p> this.addInvalidity(this.validityChecks[index].invalidityMessage);</p><p> </p><p> var requirementElement = this.validityChecks[index].element;</p><p></p><p> if (requirementElement) {</p><p> if (isInvalid) {</p><p> requirementElement.classList.add('invalid');</p><p> requirementElement.classList.remove('valid');</p><p> } else {</p><p> requirementElement.classList.remove('invalid');</p><p> requirementElement.classList.add('valid');</p><p> }</p><p> }</p><p> }[/CODE]</p><p></p><p>I used one single function:</p><p>[CODE]async function checkPhoneNumber(input, index) {</p><p> var username = 'User';</p><p> var password = 'Pass';</p><p> var url = 'http://127.0.0.1/app/api/api.php';</p><p> </p><p> var postData = {</p><p> "type" : 'GET',</p><p> "action" : 'phone_exists',</p><p> "phone" : input.value,</p><p> };</p><p> </p><p> $.ajax({</p><p> url: url,</p><p> type: 'GET',</p><p> dataType: 'json',</p><p> data: postData,</p><p> contentType: 'application/json',</p><p> beforeSend: function(xhr) {</p><p> xhr.setRequestHeader("Authorization", "Basic "+btoa(username+':'+password));</p><p> },</p><p> success: function(resultData){</p><p> if (!Array.isArray(resultData['data']) || resultData['data'].length === 0)</p><p> return true;</p><p> </p><p> var exists = resultData['data'][0];</p><p></p><p> input.CustomValidation.addAsyncInvalidity(index, exists);</p><p> }</p><p> });</p><p>}[/CODE]</p><p></p><p>And to call that function for validation:</p><p>[CODE]isInvalid: async function(input) {</p><p> const regex = RegExp(/\d{10}$/);</p><p></p><p> if (!regex.test(input.value))</p><p> return true;</p><p></p><p> checkPhoneNumber(input, 1); //0 Based</p><p></p><p> return false; //Async function will update later to set the true state of invalid</p><p>}[/CODE]</p></blockquote><p></p>
[QUOTE="JayC, post: 466020, member: 36373"] Yeah I am working on recoding this, I see what my issue is. It is expecting you to use the 'success' method to invoke the behavior you are expecting. So I am going to make an async function to control my message to the display, and hope it works, aha. [automerge]1607912205[/automerge] Got it working. I recoded the system to use the 'Success' method as recommended by about every article I could find. Here is the page now updating: (First: The entry is 10 digits, and unique) (Second: The entry is 10 digits, already taken) (Third: The entry is 10 digits, available) [ATTACH type="full"]11769[/ATTACH] Thanks for the help! Here is what I ended up doing: Added another anonymous function into my class for custom validation: Here I tell it which index to use for the validation number, and whether it is invalid or valid [CODE]addAsyncInvalidity: function(index, isInvalid){ this.addInvalidity(this.validityChecks[index].invalidityMessage); var requirementElement = this.validityChecks[index].element; if (requirementElement) { if (isInvalid) { requirementElement.classList.add('invalid'); requirementElement.classList.remove('valid'); } else { requirementElement.classList.remove('invalid'); requirementElement.classList.add('valid'); } } }[/CODE] I used one single function: [CODE]async function checkPhoneNumber(input, index) { var username = 'User'; var password = 'Pass'; var url = 'http://127.0.0.1/app/api/api.php'; var postData = { "type" : 'GET', "action" : 'phone_exists', "phone" : input.value, }; $.ajax({ url: url, type: 'GET', dataType: 'json', data: postData, contentType: 'application/json', beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic "+btoa(username+':'+password)); }, success: function(resultData){ if (!Array.isArray(resultData['data']) || resultData['data'].length === 0) return true; var exists = resultData['data'][0]; input.CustomValidation.addAsyncInvalidity(index, exists); } }); }[/CODE] And to call that function for validation: [CODE]isInvalid: async function(input) { const regex = RegExp(/\d{10}$/); if (!regex.test(input.value)) return true; checkPhoneNumber(input, 1); //0 Based return false; //Async function will update later to set the true state of invalid }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
JavaScript API - Sync Method
Top