#1
  1. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14

    Newbie:: Javascript Help, checking form values


    Hi there,

    I would like to get some help if possible.

    I currently have some simple script on my website to check that the fields are not empty, however I would likke to take it a couple of steps further.

    Firstly the script is as follows:-

    if (form.PostCode.value == "") {
    alert( "Please provide us with your Postcode for mailing purposes." );
    form.PostCode.focus();
    return false ;
    }

    Ok, with the above snippet I would like to modify this to also do a check to see if the values being entered are numeric. How would i go about doing this?

    Secondly, I need to be able to check the format of a date field to be entered as 00/00/2001, is this difficult.

    I am new here and need all the help that I can get. It would be really appreciated.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    You can use the javascript function isNaN() to test for a value being a number. Stands for 'Not a Number'!

    So:

    if(isNaN(form.PostCode.value)) {
    alert("please give a numeric value")
    }


    would work.

    If you want a date in that format, and don't want to give 3 separate fields then you'll have to substring

    form.dateField.value.substr(0, form.dateField.value.indexOf("/")) etc then run validation checks on the returned values.

    Best thing to do though is to put three input boxes with maxsize set to 2,2 and 4. Then in the page put the /'s between the boxes!
  4. #3
  5. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14
    Hey there thanks for that, you have abeen a great help, I get waht you mean about the date format, and I think the person who wants this page would possibly want the date field in one peice, as the form that is rocessing the script, uses the $key,$Value loop, and that spits it out into a list form. But I will have a look into
  6. #4
  7. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14
    Hi there his may be another silly question, but I am not really familiar with the JS structure.

    I have a field that I want someone to select, like a yes field, but if that field is ticked yes, then I need to check to see if the 'If yes please explian' field is entered, would i do that somthing like:-

    Code:
    If (form.medicalcond.value == "yes") { 
       If (form.medicalcondexplain.value == "") { 
       alert( "You must provide us with your medical history...." ); 
       form.medicalcondexplain.focus(); 
       return false ; 
       }
    }
    What that do what I am after, with the nested if statement??
  8. #5
  9. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14
    Hey there thanks for that, however, the form that I am currently using the option for yes or no is from a pulldown, not a checkbox.

    The reason for this is that the formprocessor script that I am currently using, without re-programming it does not check for a Y/n value for checked option etc.

    So I have created yes/no pulldown options from the pulldow, hence why I wanted to search on the "yes" value of the field.

    Does this make it a little more difficult, or should I use something like the

    form.medical.value.substr(0, form.medical.value.indexOf("Yes"))
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Toronto, Ontario, Canada
    Posts
    631
    Rep Power
    13
    To make the pulldown menu:
    Code:
    <form name="medcond"... >
    <select name="medical">
    <option value=1>Yes</option>
    <option value=0>No</option>
    </select>
    </form>
    Script to check wheter it is yes/no:
    Code:
    if(!document.medcond.medical.value) {
    // no is selected
    }
    else {
    // yes is selected
    }
  12. #7
  13. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14
    Hey there all thanks for all of your help with this. it is really appreciated... I will check it all works when I get back to work.

  14. #8
  15. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14
    Hey thanks for that code, it appears to look great, I can follow what you are doing, but for the checking of the date, there is alot that goes into it huh?

    As an example, if I were to change to format of the date to Aussie, what parts of the date check would i be required to change? Currently it is formated to US dates, but I would love to use it in aussie dates.

    And do you have an email address to give credit for the code?
  16. #9
  17. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14
    Hey there, the format for the aussie date is usually:

    dd/mm/yyyy

    And not a problem I like to give credit where credit is due.
  18. #10
  19. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    14
    hehehehehe yeah I know we all could do with the money.

    Cheers Rob, I will have a plug away when I get a chance, got some other things I am doing at the moment., will look later today.

    Cheers

    Matt aka Mystik_Web

IMN logo majestic logo threadwatch logo seochat tools logo