1

One of my websites I am working on, http://www.michaelgworkman.com, is one that I created to help me get a job, and also help my career in general.

In have been trying to add a lightweight JQuery form validation for the contact form on this website, which is hosted on Microsoft Azure Cloud Platform and having problems. Referencing the drop down list for INTEREST in the form is always returning FALSE form when an option is not selected, and also returning FALSE when I select an item in the drop down list. Here is what the form looks like:

enter image description here

I have this in my form in the Razor view .cshtml file for my form:

enter image description here

And this is the HTML generated from the form:

enter image description here

When I submit the form without selecting an interest in the INTEREST drop down, it correctly returns a false from the JQuery in the file.

enter image description here

When I submit the form after selecting an interest, the JQuery still says an option has not been selected in the drop down list, this is how the form looks after selecting a value:

enter image description here

Here is the JQuery code that is always returning false even after selecting a value in the INTEREST drop down list.

enter image description here

I would like to know how to fix this problem, it has been stumping me for about an hour so far.

2 Answers 2

1

Ok I found the problem here, I was using a wrong ID for the drop down list in the form, I was trying to use $("#ContractCategory"), but that is not the correct ID, the Razor code is automatically creating the ID as id="ContractCategories_ID", based on the name of the database table being used by the Entity Framework in this ASP.NET MVC Web Application. Simple fix, a little surprised I was working on this for about an hour, now the form works correctly.

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

Comments

0

For one thing, you don't have an element with id="ContactCategory" in the markup you're showing us, so if that selector isn't finding anything then false may indeed be the correct result. Try:

alert($('#ContactCategories_ID option').is(':selected'));

But even then, since that selector finds multiple elements then the semantics of what you're doing still may not make much sense and could return unexpected results. Instead of looking for a selected option, just check the value of the form element itself. Nomrally to get the value you might do something like:

let category = $('#ContactCategories_ID').val();

And check that value:

alert(category === '');

1 Comment

Yes, that is exactly the source of the error to this problem, an incorrect ID of the form element, I just realized it after making my post, but thank you for pointing it out for me, a simple fix.

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.