Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validation (Java)

    I am very new to JAVA but have some experience in HTML. For learning experience, I simply want to validate a form. Would really appreciate your advice and your example. If possible, the script with comments so I know what is going on.


    Here is my form:
    <FORM METHOD="POST"
    ACTION="mailto:szather@ouray.com"
    ENCTYPE="text/plain"
    onSubmit="return valid(this);">

    <strong>Your Email:</strong>
    <input type="text" name="email">
    <br>
    <strong>Date:</strong>
    <input type="text" name="date">
    <br>
    <strong>Time:</strong>
    <input type="text" name="time">
    <strong><br>
    <br>
    Please write the issue:</strong><br>
    <textarea name="problem" COLS="80" ROWS="10"></textarea>
    <br>
    <br>
    <INPUT TYPE="submit" VALUE="Submit">
    </FORM>


    The validation:
    - Email address = the email address has an "@" and a "." following the "@" sign.
    - Date = date is entered in the format 03/15/2007
    - Time = e.g. in the form 3:35 PM or 11:02 AM
    - Please write the issue = problem description is at least 2 characters long

    <SCRIPT> LANGUAGE="JavaScript"> I believe is a good start.. (help please).. Any example of a very simple script, with comments if possible. I checked the script on many sites, and I am still confused with the code. Thanks for any help..

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    First of all, welcome to Coding Forums...

    Second, Java and Javascript are 2 entirely different scripting languages....This is a Javascript forum, so I am offering you a Javascript solution...

    Please study the following code and try to learn something from it....generally, I usually only help people who have made an effort in scripting their own javascript.

    The bulk of the form validation I acheived with regular expressions, as follows:

    Code:
    <FORM METHOD="POST"
    ACTION="mailto:szather@ouray.com"
    ENCTYPE="text/plain"
    name="form1"
    onSubmit="return valid(document.form1);">
    
    <strong>Your Email:</strong>
    <input type="text" name="email">
    <br>
    <strong>Date:</strong>
    <input type="text" name="date">
    <br>
    <strong>Time:</strong>
    <input type="text" name="time">
    <strong><br>
    <br>
    Please write the issue:</strong><br>
    <textarea name="problem" COLS="80" ROWS="10"></textarea>
    <br>
    <br>
    <INPUT TYPE="submit" VALUE="Submit">
    </FORM>
    
    
    <script type="text/javascript">
    
    function valid(formObj)
    {
        if (formObj.email.value.length > 0)
        {
            var myRegxp = /(.+?)(@)(.+?)(\.)(.+?)/;
               if (!myRegxp.test(formObj.email.value))
               {
                      alert('Email was entered incorrectly');
                      formObj.email.focus();
                      return false;
               }
        }
        else
        {
                  alert('Email was entered incorrectly');
                  formObj.email.focus();
                  return false;
        }
    
        if (formObj.date.value.length > 0)
        {
            var myRegxp2 = /(\d{2})(\/)(\d{2})(\/)(\d{4})/;
               if (!myRegxp2.test(formObj.date.value))
               {
                      alert('Date was entered incorrectly');
                      formObj.date.focus();
                      return false;
               }
        }
        else
        {
                  alert('Date was entered incorrectly');
                  formObj.date.focus();
                  return false;
        }
    
        if (formObj.time.value.length > 0)
        {
            var myRegxp3 = /(\d{1,2})(:)(\d{2})( )(AM|PM)/;
               if (!myRegxp3.test(formObj.time.value))
               {
                      alert('Time was entered incorrectly');
                      formObj.time.focus();
                      return false;
               }
        }
        else
        {
                  alert('Time was entered incorrectly');
                  formObj.time.focus();
                  return false;
        }
    
        if (formObj.problem.value.length < 2)
        {
                  alert('Problem was entered incorrectly');
                  formObj.problem.focus();
                  return false;
        }
    
        return true;
    }
    
    </script>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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