pattern不使用动态附加HTML元素

时间:2013-11-18 12:31:27

标签: css html5

我正在动态创建HTML元素,点击按钮将其附加到div。我的问题是HTML模式验证不适合...

 var moredetail="<label for='teachername' data-icon='' class='nopostcard'> Teacher\'s Name 
</label> <input type='text' id='teachername' class='nopostcard' required='required' pattern='[A-Za-z]' 
title='TeacherName must be alphanumeric' />";
        moredetail +="<label for='comment' data-icon='' class='nopostcard'> Comments </label>
 <input type='text' id='comment' class='comment nopostcard' required='required'/>";

2 个答案:

答案 0 :(得分:1)

我正在为html验证创建按钮类型=“按钮”我应该让它提交类型, 如果我使用提交,我有一些问题。所以我使用以下代码获得了按钮

var String = document.getElementById("teachername").value;
var Letters = /^[a-zA-Z\u00C0-\u00ff]+$/.test(String);

 if(Letters == false)
 {
 alert('Teacher name can be only string');
 return;
 }

答案 1 :(得分:0)

尝试给模式属性赋予长度 例如:

 <label for='teachername' data-icon='' class='nopostcard'> Teacher\'s Name </label> 
    <input type='text' id='teachername' class='nopostcard' required='required' 
    pattern='[A-Za-z]{5}' 
    title='TeacherName must be alphanumeric' />

此代码将检查5个字符的模式长度