Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

FORM : Required fields

  • 12-02-2006 09:11PM
    #1
    Registered Users, Registered Users 2 Posts: 1,667 ✭✭✭


    This gives me an headache. I tried this script on IE and Firefox, it works except on Firefox it still sends out the form action. I am working on a credit card payment page, so on Firefox it does give the user warning to field the required fill but once click OK, it sends out the data.
    <HTML>
    <HEAD>
    <TITLE>Required Fields</TITLE>
    <SCRIPT>
    function validate() {
    mNv=mainform.Name.value;
    if (mNv=='') {
    alert('Your name is a required field. Please try again.');
    event.returnValue=false;
    }
    if (!(mainform.Sex[0].checked || mainform.Sex[1].checked)) {
    alert('Your sex is a required field. Please try again.');
    event.returnValue=false;
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="mainform" ACTION="http://yourdomainhere/cgi-bin/post-query" METHOD="post" onsubmit="validate();">
    <INPUT TYPE="TEXT" NAME="Name" >Please enter your name (required)
    <BR>
    <INPUT TYPE="RADIO" NAME="Sex" VALUE="Male">Male
    <INPUT TYPE="RADIO" NAME="Sex" VALUE="Female">Female
    <BR>
    <INPUT TYPE="SUBMIT">
    </FORM>
    </BODY>
    </HTML>
    

    am wondering if anyone has better script to do this?


Comments

  • Closed Accounts Posts: 7,144 ✭✭✭DonkeyStyle \o/


    Well you could just hand off that task to the form handler script... which is probably better anyway since if someone is determined enough to bypass these 'required fields' then a bit of javascript isn't going to stop them.
    This is assuming you've actually got access to it though.


  • Registered Users, Registered Users 2 Posts: 1,667 ✭✭✭MartMax


    i'm basically creating a form that collect credit card details to be passed to my merchant gateway. i only need a simple script that ensure all required data is sent to the server. i don't need bullet proof script, a SSL cert is already in place, not that i say it's gonna be 100% secured transmission.

    SORTED, found another working script.
    <script language="JavaScript" type="text/javascript">
    <!--
    function validateForm() {
     var name = document.myForm.username.value;
     var email = document.myForm.email.value;
     if (name == "") {
       alert("Please fill in your name.");
       return false;
     }
     if (email == "") {
       alert("Please fill in your email address.");
       return false;
     }
     return true;
    }
    //-->
    </script>
    <form action="blah.cgi" name="myForm" method="post"
         onSubmit="return validateForm();">
    Your name: <input type="text" name="username"> *<br>
    Email address: <input type="text" name="email"> *<br>
    Phone Number: <input type="text" name="phone"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    <p>(* indicates a required field)</p>
    


Advertisement