日期:2014-05-16  浏览次数:20321 次

js 验证表单
<script language="javascript" type="text/javascript">
function Formfield(name, label){
this.name=name;
this.label=label;
}
function verifyForm(objForm){
var list  = new Array(new Formfield("name", "产品名称"),new Formfield("typeid", "产品类型"),
new Formfield("baseprice", "产品底价"),new Formfield("marketprice", "产品市场价")
,new Formfield("sellprice", "产品销售价"),new Formfield("description", "产品描述"),
new Formfield("stylename", "产品图片的样式"),new Formfield("imagefile", "产品图片"));
for(var i=0;i<list.length;i++){
var objfield = eval("objForm."+ list[i].name);
if(trim(objfield.value)==""){
alert(list[i].label+ "不能为空");
if(objfield.type!="hidden" && objfield.focus()) objfield.focus();
return false;
}
}
var imagefile = objForm.imagefile.value;
var ext = imagefile.substring(imagefile.length-3).toLowerCase();
if (ext!="jpg" && ext!="gif" && ext!="bmp" && ext!="png"){
alert("只允许上传gif、jpg、bmp、png!");
return false;
}
    return true;
}
function SureSubmit(objForm){
if (verifyForm(objForm)) objForm.submit();
}
</script>

<input type="button" name="Add" value=" 确 认 " class="frm_btn" onClick="javascript:SureSubmit(this.form)">


<script language="javascript" type="text/javascript">
function trim(s) {
var tempStr;
tempStr = s.replace(/\s+$/g,'');
tempStr = tempStr.replace(/^\s+/g,'');
return tempStr;
}
//验证是否为空
function isEmpty(s){
return ((s == null) || (trim(s).length == 0))
}

function isNum(s) {
    var str =/^[0-9]*$/;
    return (str.test(s));
}
</script>