0

Anyway I've tried pretty hard but I'm still struggling.

Essentially what I need to do is validate a form for an assignment.

I need to make sure the email format is valid, which I assume only includes ".", "@", numbers and letters.

I need to make sure one part of the form only accepts alpha numeric characters

I need to make sure at least one box has been ticked

and as I said in the title, make sure the expiry date box only accepts a "/" or numbers.

Any help or tips would be hugely appreciated because I'm absolutely stuffed right now. Thanks.

gillon
  • 1
  • Possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Mosh Feu Mar 17 '16 at 10:46
  • Reference here : https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation . Please come back with a more precise question. – alpha1554 Mar 17 '16 at 10:49

1 Answers1

0

Validating email

<input pattern="/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required />

I need to make sure one part of the form only accepts alpha numeric characters

<!DOCTYPE html>
<html>
   <head>
      <title>Validate</title>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width">
      <style>
         .widefat-main{
         }
      </style>
      <script>
         function validate() {
            var errorDiv = document.getElementById("errorDiv"),
                    regex = /^[a-z0-9]+$/,
                    str = document.getElementById("inputString").value;

            if ((str.length > 4) && (str.length < 10) && regex.test(str)) {
               errorDiv.innerHTML = "Fine string";
               return true;
            }
            else {
               errorDiv.innerHTML = "4 to 10 alphanumerical characters only";
               return false;
            }
         }
      </script>
   </head>
   <body>
      <form action="" onsubmit="return validate();">
         <input 
            id="inputString"
            type="text" 
            name="url_code" 
            class="widefat-main" 
            title="4 to 10 alphanumerical characters only" 
            />
         <input type="submit" value="Submit"/>
      </form>
      <div id="errorDiv"></div>
   </body>
</html>

I need to make sure at least one box has been ticked

var f = document.form1;
if (f.summer.checked || f.autumn.checked || f.winter.checked) {
    // we're OK!
}

and as I said in the title, make sure the expiry date box only accepts a "/" or numbers.

var errormessage = "";
 function checkIsValidDateTime(){
      errormessage = "";
      var isvalid = false;
      var dt = document.getElementsByName("date")[0].value;
      var parts = dt.split(" ");
      if(parts.length == 3){
         var date = parts[0];
         var time = parts[1];
         var ampm = parts[2];
         if(ampm.length == 2 && (ampm.toLowerCase() == "am" || ampm.toLowerCase() == "pm")){
            var validformatdate=/^\d{2}\/\d{2}\/\d{4}$/;
            if (validformatdate.test(date)){
                var validformattime = /^\d{1,2}:\d{2}$/;
                if (validformattime.test(date)){
                     isvalid = true;
                }else{errormessage = "Time is not in the format HH:MM";}
            }else{errormessage = "Date is not in the format dd/mm/yyyy";}
         }else{errormessage = "DateTime does not have AM/PM at the end";}
      }else{errormessage = "DateTime is not in the format 11/07/2011 3:45 AM";}
      return isvalid;
 }
  • just to clarify, is this language what client side scripting is? or is it this for eg. ? if (document.order.fname.value=="") { alert("invalid") document.order.fname.focus(); return false sorry! I really dont know much – gillon Mar 17 '16 at 11:04
  • @gillon yes these are client side scripting (javascript). [Click here](http://www.w3schools.com/js/default.asp) to know more... – Vishva Nath Singh Mar 17 '16 at 11:08
  • one other query vishva, for example, this is what the email part of the html says
    what would you add in to your email bit to fit it with this? thanks again and sorry for the trouble!
    – gillon Mar 17 '16 at 12:33
  • and same with alpha numeric except with this code :
    – gillon Mar 17 '16 at 12:35