Web Analytics Made Easy -
StatCounter Form Input boolean error checking - CodingForum

Announcement

Collapse
No announcement yet.

Form Input boolean error checking

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form Input boolean error checking

    Hi,

    I'm dealing with a simple form with mixed radiobuttons, textbox's, checkboxes etc. I'm trying to figure out an efficient way to perform value checking. To make sure all the questions have answered, here's what I was going for this however now the value data is occupied regardless by crap that gets passed through to the handler script.

    -----------------------------------------------------------------
    <!--
    function checkData()
    {
    var correct = true
    if (document.id.1.value == "") {correct = false; alert("Please enter your name.")}
    if (document.id.2.value == "") {correct = false; alert("Please enter your e-mail address.")}

    if (correct){alert("Message being sent, standby for transfer confirmation.")}
    return correct
    }
    //-->

    <form onSubmit="return checkData()" name="id"
    -----------------------------------------------------------------


    If someone could please enlighten me as to proper syntax to replace value == "" to testing whether there's an input value or not (1 or 0 I'm thinking) so I could invoke the alert box if there is nothing answered on each input-type I'd be much appreciative!

  • #2
    some methods of validation

    //for a checkbox or radio button
    if (document.yourformname.yourcheckboxname.checked==true){
    alert('this checkbox has been checked');
    }

    if (form.yourcheckboxname.checked==false){
    alert('this checkbox has not been checked');}

    //for an <INPUT type="text">
    if (document.yourformname.yourinputname.value == ""){
    alert("this field is blank");


    When validating forms I usually do this:

    in your <FORM> tag include:
    onsubmit="return validate(this);"

    function validate(form){
    //for a checkbox or radio button
    if (form.yourcheckboxname.checked==true){
    alert('this checkbox has been checked');}

    elseif (form.yourcheckboxname.checked==false){
    alert('this checkbox has not been checked');
    }

    //for an <INPUT type="text">
    elseif (form.yourinputname.value == ""){
    alert("compulsory field: enter some data");
    form.yourinputname.focus();
    // .focus places the cursor in the field you wish for them to complete,
    // which is good for forcing the order of completion...
    return false;}

    return true()
    }//end function

    You should have little trouble in tailoring this example for your form.

    I hope this post is of assistance to you.

    - Andrew

    Comment


    • #3
      you're the man, I'd throw ya a beer but we havent got that technology yet, cheers either way and thanks again amigo!

      Comment

      Working...
      X