validate.js

function getElementByName(name){
 var elements = document.getElementsByName(name);
 return elements[0];
}
function validate(){
 var element = getElementByName("userName");
 if(element.value.length == 0){
  alert("Please input userName!");
  element.focus();
  return false;
 }
 element = getElementByName("passwd");
 if(element.value.length == 0){
  alert("Please input passwd!");
  element.focus();
  return false;
 }
 element = getElementByName("categoryName");
 if(element.value.length == 0){
  alert("Please input categoryName!");
  element.focus();
  return false;
 }
 element = getElementByName("categoryDescn");
 if(element.value.length == 0){
  alert("Please input descn!");
  element.focus();
  return false;
 }

// element = getElementByName("bookPrice");
// if(element.value.length == 0){
//  alert("Please input bookPrice!");
//  element.focus();
//  return false;
// }
// else if(isNaN(element.value)){
//  alert("BookPrice must be number!");
//  element.focus();
//  return false;
// }
// var arr = element.value.split(".");
// if(arr[0].length>5){
//  alert("Integral part must <= 5!");
//  element.focus();
//  return false;
// }
// else if(arr[1].length>2){
//  alert("Decimal digits must <= 2!");
//  element.focus();
//  return false;
// }
}

你可能感兴趣的:(function,input)