表单验证继续失败。我究竟做错了什么?

时间:2012-06-13 02:50:11

标签: javascript validation

<script type="text/javascript">
function doSubmit() {
  if (submitPhone() == false){
    return false;
  }

  if (submitEmail() == false){
    return false;
  }

  if (submitZip() == false){
    return false;
  }

  if (validateText() && validateRadio()) {
    var confirmation = "";
    confirmation += "<h1>Your order has been submitted.</h1><br/><br/>";
    confirmation += "<h3>Order Destination</h3><br/>";
    confirmation += "Name: " + document.AddressForm.customer.value + "<br />";
    confirmation += "Address: " + document.AddressForm.address.value + "<br />";
    confirmation += "City: " + document.AddressForm.city.value + "<br />";

    var ind = document.AddressForm.state.options.selectedIndex;

    confirmation += "State: " + document.AddressForm.state.options[ind].value + "<br />";
    confirmation += "Zip: " + document.AddressForm.zip.value + "<br />";
    confirmation += "Phone: " + document.AddressForm.phone.value + "<br />";
    confirmation += "Email: " + document.AddressForm.email.value + "<br />";

    confirmation += "<hr> <br/> <h3>Order Specifications</h3><br/>";

    var order = printRadio();

    confirmation += order;

    document.write(confirmation);
  }
  return;
}

function validateText() {
  var transcribed = true;

  var notification = "";

  var customer = document.AddressForm.customer.value;
  if (customer.length == 0) 
  {
    transcribed = false;
    notification += "Please enter your name.\n";
  }
  var address = document.AddressForm.address.value;
  if (address.length == 0) 
  {
    transcribed = false;
    notification += "Please enter your address.\n";
  }
  var city = document.AddressForm.city.value;
  if (city.length == 0) 
  {
    transcribed = false;
    notification += "Please enter your city.\n";
  }
  var state = document.AddressForm.state.value;
  if (state.length == 0) 
  {
    transcribed = false;
    notification += "Please enter your state.\n";
  }
  var zip = document.AddressForm.zip.value;
  if (zip.length == 0) 
  {
    transcribed = false;
    notification += "Please enter your zip.\n";
  }
  var phone = document.AddressForm.phone.value;
  if (phone.length == 0) 
  {
    transcribed = false;
    notification += "Please enter your phone number.\n";
  }
  var email = document.AddressForm.email.value;
  if (email.length == 0) 
  {
    transcribed = false;
    notification += "Please enter your email.\n";
  }

  if (!transcribed)
  {
    alert(notification);
  }

  return transcribed;
}

function validateRadio() {
  var highlighted = false; 

  if (document.ComputerForm.cases[0].checked) {
      highlighted = true;
  }
  if (document.ComputerForm.cases[1].checked) {
      highlighted = true;
  }
  if (document.ComputerForm.cases[2].checked) {
      highlighted = true;
  }

  if (document.ComputerForm.monitors[0].checked) {
      highlighted = true;
  }
  if (document.ComputerForm.monitors[1].checked) {
      highlighted = true;
  }
  if (document.ComputerForm.monitors[2].checked) {
      highlighted = true;
  }

  if (document.ComputerForm.printers[0].checked) {
      highlighted = true;
  }
  if (document.ComputerForm.printers[1].checked) {
      highlighted = true;
  }
  if (document.ComputerForm.printers[2].checked) {
      highlighted = true;
  }

  if (!highlighted) {
      alert("Please complete your order.");
  }

  return highlighted;
}

function printRadio() {
  var order;
  var order1;
  var order2;
  var order3;

  if (document.ComputerForm.cases[0].checked) {
    order1 = "Case: Desktop Case <br />";
  }
  if (document.ComputerForm.cases[1].checked) {
      order1 = "Case: Mini-Tower Case <br />";
  }
  if (document.ComputerForm.cases[2].checked) {
      order1 = "Case: Full-Tower Case <br />";
  }

  if (document.ComputerForm.monitors[0].checked) {
      order2 = "Monitor: 17 inch LCD Flat Screen <br />";
  }
  if (document.ComputerForm.monitors[1].checked) {
      order2 = "Monitor: 19 inch LCD Flat Screen <br />";
  }
  if (document.ComputerForm.monitors[2].checked) {
      order2 = "Monitor: 21 inch LCD Flat Screen <br />";
  }

  if (document.ComputerForm.printers[0].checked) {
      order3 = "Printer: Inkjet Printer <br />";
  }
  if (document.ComputerForm.printers[1].checked) {
      order3 = "Printer: Laser Printer <br />";
  }
  if (document.ComputerForm.printers[2].checked) {
      order3 = "Printer: Color Printer <br />";
  }

  order = order1 + order2 + order3;

  return order;
}

function submitPhone() {    
  if(validatePhone() == true) {
    alert("Phone number entered correctly.");
  } else {
    var message = "Phone number must be in one of the following formats:\n";
    message += "123-456-7890\n";
    message += "1234567890\n";
    message += "(123)4567890\n";
    message += "(123)456-7890\n";

    alert(message);
  }
}

function validatePhone(){
  var phone = document.AddressForm.phone.value;

  if(isTenDigits(phone) == true || isTwelveAndDashes(phone) || isTwelveAndPara(phone) || isThirteenDashPara(phone))
    return true;

  return false;
}

//Validates phone number with dashes. 
function isTwelveAndDashes(phone) {
  if(phone.length != 12)
    return false;

  var pass = true;

  for (var i = 0; i < phone.length; i++){
    var c = phone.charAt(i);

    if(i == 3 || i == 7){
      if (c != '-'){
        pass = false;
      }
    } else {
      if (!isDigit(c)){
        pass = false;
      }
    }
  }

  return pass;
}

//Validates area code in paratheses phone number. 
function isTwelveAndPara(phone) {
  if(phone.length != 12)
    return false;

  var pass = true;

  for (var i = 0; i < phone.length; i++){
    var c = phone.charAt(i);

    if(i == 0){
      if(c != '('){
        pass = false;
      }
    } else if (i == 4){
      if(c != ')'){
        pass = false;
      }
    } else {
      if (!isDigit(c)){
        pass = false;
      }
    }

  }
  return pass;
}

//Validates a phone number with paratheses and a dash.
function isThirteenDashPara(phone) {
  if(phone.length != 13)
    return false;

  var pass = true;

  for (var i = 0; i < phone.length; i++){
    var c = phone.charAt(i);

    if (i == 0){
      if (c != '('){
        pass = false;
      }
    } else if (i == 4){
      if (c != ')'){
        pass = false;
      }
    } else if (i == 8){
      if (c != '-'){
        pass = false;
      }
    } else {
      if (!isDigit(c)){
        pass = false;
      }
    }
  }

  return pass;
}

//Validates an all numbers phone number.
function isTenDigits(phone) {
  if(phone.length != 10)
    return false;

  var pass = true;

  for (var i = 0;i < phone.length; i++){
    var c = phone.charAt (i);
    if (!isDigit(c)){
      pass = false;
    }
  }

  return pass;
}

//Validates whether or not all characters within the phone number entry are numbers.
function isDigit(num) {
  if (num.length != 1){return false;}
  var string="1234567890";

  if (string.indexOf(num)!= -1){
    return true;
  }

  return false;
}

function submitEmail() {
  if(validateEmail() == true) {
    alert("Email entered correctly.");
  } else{
    var message = "Email must contain an @ sign:\n @ sign cannont be the first or last character.";

    alert(message);
  }
}

//Validates Email entry. Validation is looking for an '@' sign within the email address, which must not be either the first or last character. 
function validateEmail() {

  var email = document.AddressForm.email.value;

  var pass = true;

  var atInd = email.indexOf("@");


  var lastChar = email.charAt(email.length - 1);

  if(atInd == -1) { // no @
    pass = false;
  } else if(atInd == 0) { //the first is @
    pass = false;
  } else{
    if(lastChar == '@') {//the last is @
      pass = false;
    }
  }

  return pass;
}

function submitZip() {
  if(validateZip() == true) {
    alert("Zip code entered correctly.");
  } else{
    var message = "Zip code must be 5 digits long and contain only numbers.";
    alert(message);
  }
}

function validateZip() {
  var zip = document.AddressForm.zip.value;

  if(digits(zip) == true) {
    return true;
  }

  return false;
}

//Validates zip code entry length and that all characters are numerical.                
function digits(zip) {
  if(zip.length != 5)
    return false;

  var pass = true;

  for (var i = 0;i < zip.length; i++){
    var c = zip.charAt (i);

    if (!isDigit(c)){
      pass = false;
    }
  }

  return pass;
}
</script>

0 个答案:

没有答案