www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript validation

  1. #1
    Join Date
    Dec 2006
    Posts
    2

    javascript validation

    hi i have a problem with my javascript and ajax i try to get it to validate the date but it fails each time. here is the code i used
    <table width='80%' border =1>

    <script language="Javascript">

    var xmlHttpReq = false;

    function callback ()
    {
    if (xmlHttpReq.readyState == 4)
    {
    alert("Inserted into db continuing to step 2");
    document.location.href="step2hire.php"
    }



    }

    function xmlhttpPost()
    {
    // Mozilla/Safari
    if (window.XMLHttpRequest)
    {
    xmlHttpReq = new XMLHttpRequest ();
    }
    // IE
    else if (window.ActiveXObject)
    {
    xmlHttpReq = new ActiveXObject ("Microsoft.XMLHTTP");
    }

    xmlHttpReq.open ('GET', "assmakeahire.php?hire_id="+document.myajax.hire_id.value
    + "&hire_date=" +document.myajax.hire_date.value
    + "&due_return_date=" +document.myajax.due_return_date.value
    + "&pick_up_date=" +document.myajax.pick_up_date.value
    + "&available_hire_date_again=" +document.myajax.available_hire_date_again.value
    + "&location_return_fk_depot_id=" +document.myajax.location_return_fk_depot_id.value
    + "&location_pickup_fk_depot_id=" +document.myajax.location_pickup_fk_depot_id.value
    + "&staff_fk_person_id=" +document.myajax.staff_fk_person_id.value
    + "&customer_fk_person_id=" +document.myajax.customer_fk_person_id.value
    + "&notes=" +document.myajax.notes.value
    + "&total_days_on_hire=" +document.myajax.total_days_on_hire.value, true);
    xmlHttpReq.onreadystatechange = CheckDate;
    xmlHttpReq.send(null);
    }

    function CheckDate(x){

    str = x.Date.value;
    str = str.substring();

    if(str.indexOf("-")== 4 && str.length == 10){
    str = str.split("-");
    if(str[0].length < 5){ years = true;}else{years= false;}
    if(str[1] < 13){months = true;}else{months = false;}
    if(str[2] < 32){days = true;}else{days=false;}

    if(years && months && days)
    {
    alert("OK")
    return true;
    }
    else{
    alert("NOT OK");
    return false;
    }
    }
    else{
    alert("Not a valid Date");
    return false;
    }

    }

    </script>
    </head>
    <h1>Step 1:Hiring a Vehicle</h1>
    <p>Please enter the hire details remember only 1, 3 or 7 or more days are allowed</p>
    <form name=myajax >
    <table border =0>
    <tr><td><b>Hire ID:</b> </td><td><input type="text" name="hire_id"><br><td></tr>
    <tr><td><b>Hire date:</b> </td><td><input type="text" name="hire_date"><br><td></tr>
    <tr><td><b>Returning vehilce:</b> </td><td><input type="text" name="due_return_date"><br><td></tr>
    <tr><td><b>Collecting Vehicle:</b> </td><td><input type="text" name="pick_up_date"><br><td></tr>
    <tr><td><b>Vehicles avaliable again:</b> </td><td><input type="text" name="available_hire_date_again"><br><td></tr>

    <tr><td><b>Depot returning it to:</b> </td><td><select name="location_return_fk_depot_id">
    <option value = "N">N</option>
    </select><br><td></tr>
    <tr><td><b>Depot collecting it from:</b> </td><td><select name="location_pickup_fk_depot_id"><br>
    <option value = "N">N</option>
    </select><br><td></tr>
    <tr><td><b>Staff ID:</b> </td><td><select name="staff_fk_person_id">
    <option value="S001">S001</option>
    <option value="S002">S002</option>
    <option value="S003">S003</option>
    </select><br><td></tr>
    <tr><td><b>Customer ID:</b> </td><td><input type="text" name="customer_fk_person_id"><br><td></tr>
    <tr><td><b>Notes:</b> </td><td><input type="text" name="notes"><br><td></tr>
    <tr><td><b>Days on hire:</b> </td><td><input type="text" name="total_days_on_hire"><br><td></tr>
    <tr><td><input value="insert" type="button" onclick='JavaScript:xmlhttpPost()'><td></tr>
    </form>
    </table>



    please help this prob is very frustrating
    kezmacgov thanks for your help in advance

  2. #2
    Join Date
    Apr 2006
    Location
    Edinburger, Scotland
    Posts
    107
    I copied/pasted your code here into HTML-Kit, and clicked on the insert button. Where you have
    Code:
    str = x.Date.value;
    , it says that Date is null or not an object, so I'm guessing this is your problem. I've not looked at your code properly so I couldn't tell you how to fix this.

  3. #3
    Join Date
    Dec 2006
    Posts
    2

    thanks

    thanks for your help. i came accross the same problem when i tried to run it. although it works when you run the code on its own like this

    <html>
    <head>
    <title>Date validate</title>
    <script language="JavaScript">
    <!--
    function CheckDate(x){

    str = x.Date.value;
    str = str.substring();

    if(str.indexOf("-")== 4 && str.length == 10){
    str = str.split("-");
    if(str[0].length < 5){ years = true;}else{years= false;}
    if(str[1] < 13){months = true;}else{months = false;}
    if(str[2] < 32){days = true;}else{days=false;}

    if(years && months && days)
    {
    alert("OK")
    return true;
    }
    else{
    alert("NOT OK");
    return false;
    }
    }
    else{
    alert("Not a valid Date");
    return false;
    }

    }
    //-->
    </script>
    </head>
    <body>
    <form name="date" onSubmit="return CheckDate(this)">
    <input type="text" name="Date" value="">
    <input type="Submit" value="Checkdate">
    </form>
    </body>
    </html>

    but when i nest it in the code it throws up lots of errors
    thanks againfor your help

    kezmacgov

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles