Easy HTML Question...

Discussion in 'OT Technology' started by venger240, Nov 4, 2005.

  1. venger240

    venger240 Hoemoe

    Joined:
    Oct 20, 2003
    Messages:
    2,938
    Likes Received:
    0
    Location:
    Nashville, TN
    I need to make an input field required before someone can submit it in a form.

    Here is an example of the code..

    PHP:
    <form name="myform" action="whateve.php" method="POST">        
    <
    INPUT type="radio" name="body0" value="2">
    <
    INPUT type="radio" name="body0" value="0">
    <
    INPUT type="submit" name="submit" value="submit">
    </
    form
     
  2. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    You need to check to see if it has it on the recieving page with php. That or use javascript on the current one :greddy:
     
  3. venger240

    venger240 Hoemoe

    Joined:
    Oct 20, 2003
    Messages:
    2,938
    Likes Received:
    0
    Location:
    Nashville, TN

    You wouldnt happen to have some form of script available do you?
     
  4. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    No, you should be able to google around and find a script site with a decent one. I don't know how computer literate that you are, but it should be a relatively simple javascript.

    Otherwise maybe someone else will post one up for ya...
     
  5. venger240

    venger240 Hoemoe

    Joined:
    Oct 20, 2003
    Messages:
    2,938
    Likes Received:
    0
    Location:
    Nashville, TN
    I have been allover javascripts.com but they really arent to much help. There are a couple, but im no where close to knowing enough about javascript to be able to decipher it and they are overly complicated. I just need something that will nto let someone advance until all fields have been selected.
     
  6. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,922
    Likes Received:
    11
    Location:
    Los Angeles
    I wrote a PHP script that has automated form handling. :o :wiggle:

    Instead of making a useless comment, I'll help you out.

    Download and copy this JavaScript, rename it to whatever.js
    PHP:
    // email

    function checkEmail(strng) {
    var 
    error="";
    var 
    emailFilter=/^.+@.+\..{2,3}$/;
    if (!(
    emailFilter.test(strng))) { 
        
    error "Please enter a valid email address.\n";
    }else {
        
    //test email for illegal characters
        
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
        if (strng.match(illegalChars)) {
            error = "
    The email address contains illegal characters.\n";
        }
    }
    return error;    
    }

    // dates

    function checkDate(strng) {
    // YYYY-MM-DD
    // Also separates date into month, day, and year variables
    var error = "";
    var datePat = /^(\d{4})(\-)(\d{2})\2(\d{2})$/;
    var matchArray = strng.match(datePat);
    if(matchArray == null){
        error = "
    Invalid date formatYYYY-MM-DD please.\n";
    }else{
        month = matchArray[3]; // parse date into variables
        day = matchArray[4];
        year = matchArray[1];

        if (month < 1 || month > 12) { // check month range
            error = "
    Month must be between 1 and 12.\n";
        }
        else if (day < 1 || day > 31) {
            error = "
    Day must be between 1 and 31.\n";
        }
        else if ((month==4 || month==6 || month==9 || month==11) && day==31) {
            error = "
    Month "+month+" doesn't have 31 days!\n";
        }
        else if (month == 2) { // check for february 29th
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day==29 && !isleap)) {
                error = "February " + year + " doesn'
    t have " + day + " days!\n";
            }
        }
    }
    return error;
    }

    //times

    function checkTime(strng) {
    // HH:MM:SS
    // Also separates time into hours, minutes, and seconds variables
    var error = "";
    var timePat = /^(\d{2})(\:)(\d{2})\2(\d{2})$/;
    var matchArray = strng.match(timePat);
    if(matchArray == null){
        error = "
    Invalid time formatHH:MM:SS please.\n";
    }else{
        mins = matchArray[3]; // parse time into variables
        secs = matchArray[4];
        hrs = matchArray[1];

        if (hrs < 0 || hrs > 23) { // check hour range
            error = "
    Hours must be between 0 and 23.\n";
        }
        else if (mins < 0 || mins > 59) {
            error = "
    Minutes must be between 0 and 59.\n";
        }
        else if (secs < 0 || secs > 59) {
            error = "
    Seconds must be between 0 and 59.\n";
        }
    }
    return error;
    }

    // phone number - strip out delimiters and check for 10 digits

    function checkPhone(strng) {
    var error = "";
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
        error = "
    The phone number contains illegal characters.";
    }
    if (stripped.length != 10) {
        error = "
    The phone number is the wrong lengthMake sure you included an area code.\n";

    return error;
    }

    // zip - strip out delimiters and check for 5-9 digits

    function checkZip(strng) {
    var error = "";
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
        error = "
    The zip code contains illegal characters.";
    }
    if (stripped.length < 5 || stripped.length > 9) {
        error = "
    The zip code is the wrong length.\n";

    return error;
    }


    // password - between 6-10 chars, uppercase, lowercase, and numeral

    function checkPassword(strng) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers
    if ((strng.length < 6) || (strng.length > 10)) {
        error = "
    The password should be at least 6 charactersbut no more than 10.\n";
    }
    else if (illegalChars.test(strng)) {
        error = "
    The password contains illegal characters.\n";

    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
        error = "
    The password must contain at least one uppercase letterone lowercase letter, and one number.\n";
    }  
    return error;    
    }    


    // name - 4-15 chars, uc, lc, and underscore only.

    function checkName(strng,smin,smax) {
    var error = "";
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/; // these don't belong in names
    if ((strng.length < smin) || (strng.length > smax)) {
        error = "
    The name should be " + smin + "-" + smax + " characters in length.\n";
    }
    else if (strng.match(illegalChars)) {
        error = "
    The name contains illegal characters.\n";

    return error;
    }     

    // non-empty textbox

    function isEmpty(strng) {
    var error = "";
    if (strng.length == 0) {
        error = "
    A mandatory text area has not been filled in.\n"
    }
    return error;      
    }



    /*    -------------------Example Usage-----------------------
    <html>
    <head>
    <script src="
    valid.js"></script>
    <script language="
    javascript">
    function valid(theForm) {
        var why = "";
        why += checkEmail(theForm.email.value);
        why += checkPhone(theForm.phone.value);
        why += checkPassword(theForm.password.value);
        why += checkName(theForm.name.value,4,15);
        why += checkDate(theForm.date.value);
        why += checkTime(theForm.time.value);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }
    </script>
    </head>
    <body>
    <form name="
    aForm">
    password<input type="
    password" name="password"><br>
    date<input type="
    text" name="date">YYYY-MM-DD<br>
    email<input type="
    text" name="email">[email protected]<br>
    name<input type="
    text" name="name"><br>
    phone<input type="
    text" name="phone"><br>
    time<input type="
    text" name="time">HH:MM:SS<br>
    <input type="
    submit" onClick='valid(aForm);'><br>
    </body>
    </html>
    -----------------------------------------------------------*/


    /*----------------------------Here are the full form validators, one for each specific form in the app--------------------------------------

    function checkIt(theForm){
    var why = "";
        why += checkName(theForm..value);
        why += checkZip(theForm..value);
        why += checkPhone(theForm..value);
        why += checkDate(theForm..value);
        why += checkTime(theForm..value);
        why += checkPassword(theForm..value);
        why += checkEmail(theForm..value);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }
    */

    // CUSTOMER INPUT FORM

    function checkCust(theForm){
    var why = "";
        why += checkName(theForm.fname.value,4,20);
        why += checkName(theForm.lname.value,4,20);
        why += checkZip(theForm.zip.value);
        why += checkPhone(theForm.phone.value);
        why += checkDate(theForm.dob.value);
        why += checkEmail(theForm.email.value);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }


    // USER INPUT FORM

    function checkUser(theForm){
    var why = "";
        why += checkName(theForm.login.value,4,15);
        why += checkPassword(theForm.pass1.value);
        why += checkPassword(theForm.pass2.value);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }


    //CHANGE PASSWORD FORM

    function checkPass(theForm){
    var why = "";
        why += checkPassword(theForm.pass1.value);
        why += checkPassword(theForm.pass2.value);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }


    //CREATE EMPLOYEE (THERAPIST) FORM


    function checkEmp(theForm){
    var why = "";
        why += checkName(theForm.fname.value,4,20);
        why += checkName(theForm.lname.value,4,20);
        why += checkPhone(theForm.phone.value);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }


    //CREATE SERVICE FORM


    function checkServ(theForm){
    var why = "";
        why += checkName(theForm.name.value,4,20);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }

    //CREATE INGREDIENT FORM


    function checkIngr(theForm){
    var why = "";
        why += checkName(theForm.name.value,4,80);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }

    function checkApp(theForm){
    var why = "";
        why += checkDate(theForm.date.value);
        why += checkTime(theForm.time.value);
        if (why != "") {
            alert(why);
            return false;
        }
    return true;
    }
    Then you can impliment that using the provided tests below. You may have to dig through the code to find out what you exactly want, but it works. I implimented it on scheduling software I wrote last year

    PHP:
    <html>
    <
    head>
    <
    script src="valid.js"></script>
    <
    script language="javascript">
    function 
    valid(theForm) {
        var 
    why "";
        
    why += checkEmail(theForm.email.value);
        
    why += checkPhone(theForm.phone.value);
        
    why += checkPassword(theForm.password.value);
        
    why += checkName(theForm.name.value);
        
    why += checkDate(theForm.date.value);
        
    why += checkTime(theForm.time.value);
        if (
    why != "") {
            
    alert(why);
            return 
    false;
        }
    return 
    true;
    }
    </
    script>
    </
    head>
    <
    body>
    <
    form name="aForm">
    password<input type="password" name="password"><br>
    date<input type="text" name="date">YYYY-MM-DD<br>
    email<input type="text" name="email">foo@bar.com<br>

    name<input type="text" name="name"><br>
    phone<input type="text" name="phone"><br>
    time<input type="text" name="time">HH:MM:SS<br>
    <
    input type="submit" onClick='valid(aForm);'><br>


    </
    body>
    </
    html>
     
  7. venger240

    venger240 Hoemoe

    Joined:
    Oct 20, 2003
    Messages:
    2,938
    Likes Received:
    0
    Location:
    Nashville, TN
    Ok, looking through and testing, that isnt really what im looking for. Im looking for something that w/ill not let you go to the next page, unless you have checked every possible option on the sheet...

    Thanks for trying though...
     
  8. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,922
    Likes Received:
    11
    Location:
    Los Angeles
    PHP:
    if (!isset($_POST['var']) { 
          echo 
    "thou shall not pass";
    } else { 
    header("Location: newpage.html"); }
    :dunno:
     

Share This Page