0

I am working on creating a custom validation rule for an array of inputs for an availability calendar. I have the custom validation all setup, the issue is when there is an error in the validation rule, I cannot figure out how to make it target the right element and highlight it, mainly because of the name of the array. It goes by the following convention for my example: 'availability.0'. The problem is, Jquery won't recognize the .0 as part of a valid name.

I am using AJAX to send the form data and when there is an error, it returns back this JSON data as an example

 {"readyState":4,"responseText":"{\"availability.0\":[\"Time set is not
 valid, please enter time in the following format 10:00AM-4:00PM. If you 
have\\r\\n            multiple blocks of time available in a day, separate by 
comma in the following format 10:00am-12:00pm,2:00pm-4:00pm\"]}","responseJSON":
{"availability.0":["Time set is not valid, please enter time in the following 
format 10:00AM-4:00PM. If you have\r\n            multiple blocks of time 
available in a day, separate by comma in the following format 10:00am-
12:00pm,2:00pm-4:00pm"]},"status":422,"statusText":"Unprocessable Entity"}

Here is a photo from JSONViewer showing multiple errors:

enter image description here

I can't figure out how to make this work, I can't add the .0 at the end of availability because Jquery doesn't recognize that as valid, but if I don't, then it's not the name of the array I need. I don't really know where to go from here.

1 Answer 1

1

You can select the "availability.0" response by using square brackets and placing the string that you are targeting.

responseJSON["availability.0"]   

You can use this when selecting object properties that may have JavaScript illegal characters. Such as spaces, numbers, periods, ect.

Property accessors Add, Modify, or Access Object Literal Properties

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, but I ended up using Jquery's each method instead. That seems to work well for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.