0

I'm working on an existing site with a contact form that's using JavaScript to validate the fields. The original developer wrote the JavaScript and it isn't validating one of the fields correctly. the Field is a dropdown list and it has a default value that they don't want to be selectable, so I need it to return an invalid response if the default option is selected to force the user to make a selection.

Any help would be greatly appreciated.

// Check to make sure Area of Interest is Valid
   var areaVal = $("#req_area").val();
   var areaRegexObj = /^\s*[\w\s]*\s*$/;

   if (areaRegexObj.test(areaVal)) {
      isValid = (isValid == false) ? false : true;
   } else {
      $("#req_area").val("Please specify your Area of Interest");
      isValid = false;
   }

1 Answer 1

1

Try

var areaVal = $("#req_area option:selected").val();

if (!areaRegexObj.test(areaVal))
    $("#req_area").val("Please specify your Area of Interest");

It is a good idea to have server side validation (if not already) so that a user won't disable JS and submit the form anyway.

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

3 Comments

I would go beyond "It is a good idea" -- if you don't have server-side validation in addition to client-side validation you are open to attack.
It will have server side validation, I'm just having trouble working with the JavaScript.
Thank you for the help, I managed to solve the problem. The default option had a value associated with it, I removed the value and changed the the if statement to if (areaVal == null). and removed the areaRegexObj. Thanks!

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.