[thelist] JavaScript Validation for Pull-Down Menus and Radio Buttons

Peter-Paul Koch pp.koch at gmail.com
Wed Dec 8 10:54:07 CST 2004


> I am trying to learn how to do JS form field validation, and have hit a
> snag.  I have figured out how to validate text fields just fine, but I
> cannot figure out how to validate pull-down menus, and/or Radio
> buttons.  

You don't have to. Simply check/select one option, and you can be
certain that the server always gets a value.

The code below (sorry if it is wrapping) is working for the
> text fields:

You're checking for a value, but for radio buttons you need to see
whether they're checked.

> function isRequired(fieldobject, name) {
>      var temp = fieldobject.value;
>      var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
>      if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
>      var obj = / +/g;
>      temp = temp.replace(obj, " ");
>      if (temp == " ") { temp = ""; }
>      if (temp == "") {
>          fieldobject.style.borderColor="#FF6666";
>          fieldobject.style.borderWidth="2px";
>          fieldobject.style.borderStyle="solid";
>          window.alert("The "+name+" field is required."); return false;
>      } else {
>          return true;
>      }
> }
> 


-- 
-------------------------------------------------------------------
ppk, freelance web developer

NEW: Bug Report system:
Report bugs yourself, or comment on previously 
reported ones.
http://www.quirksmode.org/bugreports/
------------------------------------------------------------------


More information about the thelist mailing list