Jump to content

Drop Down Validation


ballouta

Recommended Posts

I already have a javascript that validates several fields.

 

the 'country' field is not working properly with the current function that validates in the same time other fields.

 

the problem is that if the user chooses Turkey for example from the list it works.

If he chooses a country name of three words or more it stucks (e.g. United Arab Emirates)

 

in my validation.js:

 


    if (isProper(frm.country.value) == false) {
        alert("Please select a country");
        return false;

 

then....

 

function isProper(string) {
  if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
        
   if (!string) return false;
   
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;

}

 

Please help

Link to comment
Share on other sites

Why do you need JavaScript validation for a select list? The select list should only contain values that are valid to begin with.

 

You would want to include server-side validation, but client-side validation of a select list is not needed unless some values in a select list are only valid based upon other values in the form.

 

For example, if it was a page to apply for a job you might have an option such as:

"Are you a current employee of XYZ Company? Yes/No"

 

Then, a new question with a select list such as:

"If not a current employee of XYZ company, how did you hear about us?

  1. "

 

In that situation it might be of value to ensure they did not select one of the values in the select list unless they answered no to the previous question (Assuming the first value is null).

 

OR ----

 

You might want to validate that the user actually selected one of the "Values" from the list if you are using a "Please select a value" as the first option. But, this is very easy to validate by ensuring the item selected is not the first value or not a specific value.

 

Also, most of your isProper() function is worthless. The condition willautomatically return true or false and the rest will not be run at all.

 

What EXACTLY are you trying to achieve? Please state in plain words what you want to validate. Such as "The country should be a string with the following characters a-z, A-Z, underscore, etc. There may be spaces, but they may not begin or end the string and they may not appear in succession."

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.