验证文本框名称
Verify Textbox Names
如何验证我的文本框?这是一个创建用来接收信息的JSP页面。
名字中不应该有数字,我可以输入"103249"的名字,文本框会接受它。此外,我如何修复姓氏?
var btn = "";
function validate(myform) {
if (btn == "submit") {
//Validate Form only on 'submit' button (not for 'clear' button)
var num = 0;
var message = "";
if(myform.firstname.value == "") {
message += "- First Name must be completed 'n";
num = 1;
}
if(myform.lastname.value == "") {
message += "- Last Name must be completed 'n";
num = 1;
}
if(myform.dateofbirth.value == "") {
message += "- Date of Birth must be completed 'n";
num = 1;
}
//RADIO BUTTON VALIDATION
var g = "";
//Loop thru each radio button, verify if "checked"
for (var i=0; i<myform.gender.length; i++) {
if (myform.gender[i].checked) {
//Changes value of "g" if a value is selected
g = myform.gender[i].value;
}
}
//If no radio button selected, "g" will still = ""
if (g == "") {
message += "- Gender must be completed 'n";
num = 1;
}
if(myform.gpa.value == "") {
message += "- GPA must be completed 'n";
num = 1;
}
//Dropdown
if(myform.education.value == "") {
message += "- Education must be completed 'n";
num = 1;
}
//Dropdown
if(myform.job.value == "") {
message += "- Occupation must be completed 'n";
num = 1;
}
//RADIO BUTTON VALIDATION
var g = "";
//Loop thru each radio button, verify if "checked"
for (var i=0; i<myform.married.length; i++) {
if (myform.married[i].checked) {
//Changes value of "g" if a value is selected
g = myform.married[i].value;
}
}
//If no radio button selected, "g" will still = ""
if (g == "") {
message += "- Married must be completed 'n";
num = 1;
}
//if(myform.spouse.value == "") {
//message += "- Spouse must be completed 'n";
//num = 1;
//}
//RADIO BUTTON VALIDATION
var g = "";
//Loop thru each radio button, verify if "checked"
for (var i=0; i<myform.children.length; i++) {
if (myform.children[i].checked) {
//Changes value of "g" if a value is selected
g = myform.children[i].value;
}
}
//If no radio button selected, "g" will still = ""
if (g == "") {
message += "- Children must be completed 'n";
num = 1;
}
//Dropdown
//Only require if previous field Children marked "Yes"
if(myform.numchild.value == "0" && g == "Yes") {
message += "- Number of Children must be completed 'n";
num = 1;
}
if(myform.childSupport.value == "") {
message += "- Child Support must be completed 'n";
num = 1;
}
if(myform.creditScore.value == "") {
message += "- Credit Score must be completed 'n";
num = 1;
}
if (num == 1) {
alert ("Please complete or correct the following required fields: 'n'n"+message);
return false;
} else {
return true;
} //end if
} //end if submit button
if (btn == "delete") {
//Confirm if want to delete
var result = confirm("Are you sure you want to permanently delete this survey?");
return result;
} //end if delete button
} //end func
//=============== TO DISABLE CCARDS AND NUMBER OF CHILDREN DROPBOXES ==================
function handleSelect(myRadio){
if(myRadio.value == "No"){
document.getElementById("cc_use").selectedIndex = 0; //reset to initial default value
document.getElementById("cc_use").disabled = true;
} else {
document.getElementById("cc_use").disabled = false;
} //end if
} //end func
function handleSelect1(myRadio1){
if(myRadio1.value == "No"){
document.getElementById("numchild").selectedIndex = 0; //reset to initial default value
document.getElementById("numchild").disabled = true;
} else {
document.getElementById("numchild").disabled = false;
} //end if
} //end func
//=============== POPULATE OCCUPATION DROPDOWN BASED ON INDUSTRY DROPDOWN SELECTION ==================
function configureDropDownList(industry, occupation) { //params: id's of sending dropdown & dropdown to change
//Categories: Create a String value of all the list items in the Java ArrayList of Categories
//It translates into a String with opening and closing brackets: [], need to delete brackets from String
var categoryListString = "<%= lstCategories %>";
categoryListString = categoryListString.replace("[","");
categoryListString = categoryListString.replace("]","");
//Split the Category String into a new Javascript array of Categories
var categoryArray = new Array();
categoryArray = categoryListString.split(", "); //Make sure it's comma + space
//Occupations: Create a String value of all the nested list items in the Java ArrayList of ArrayLists: all occup names for each category
//It translates into a String with opening and closing DOUBLE brackets: [[]], need to delete brackets from String
var occupByCatsString = "<%= loccp %>";
occupByCatsString = occupByCatsString.replace("[[","");
occupByCatsString = occupByCatsString.replace("]]","");
//Split the String into a new Javascript array
var occupationsArray = new Array();
occupationsArray = occupByCatsString.split("], [");
//Loop thru array and break into further individual nested arrays for occup names in each category
for (var i = 0; i < occupationsArray.length; i++) {
occupationsArray[i] = occupationsArray[i].split(", "); //Make sure it's comma + space
}
//alert("new occup name array index 0: "+occupationsArray[0]+", index 1: "+occupationsArray[1]);
//alert("Individ occup names in index 0, name 0: "+occupationsArray[0][0]+", name 1: "+occupationsArray[0][1]);
//Categories array (categoryArray) & Nested Occup Names Array by Category (occupationsArray)
//are parallel arrays in order to match Category names with appropriate group of Occup Names
//Loop thru list of all Categories to find selected one
//and create dropdown list of matching Occup Names for that Category
for (var i = 0; i < categoryArray.length; i++)
{
if (industry.value == categoryArray[i]) {
//Call method to create Occupation dropdown list values
//Send array of that Category's list of Occupation Names as parameter, and 'occupation' id value of select
createOccupationItems(occupationsArray[i], occupation);
}
} //end for
} //end function
function createOccupationItems(occupArr, occupation) {
//Parameters in: array of that Category's list of Occupation Names, and 'occupation' id value of select
document.getElementById(occupation).options.length = 0;
for (var i = 0; i < occupArr.length; i++) {
createOption(document.getElementById(occupation), occupArr[i], occupArr[i]);
}
} //end function
function createOption(ddl, text, value) {
var opt = document.createElement('option');
opt.value = value;
opt.text = text;
ddl.options.add(opt);
} //end function
</script>
以下是要验证的实际代码:
<div class="sameLine">
<label for="firstname">First Name:</label>
<input type="text" name="firstname" value="<%=surv.getFname()%>">
</div>
<div class="sameLine">
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" value="<%=surv.getLname()%>">
UPD:
if(myform.firstname.value.search(/[^a-zA-Z]+/) != -1) {
alert("Only characters");
}
相关文章:
- 借助asp.net验证或java脚本对多个文本进行验证
- jquery中的文本框验证
- 使用angularjs验证文本框中的电子邮件
- Javascript文本框验证不起作用
- 同一文本框中的验证程序CPF e CNPJ(ASP.NET web应用程序)
- 验证文本区域
- javascript文本验证不起作用.警报消息没有't显示
- 正在验证文本框内容
- 如何在不通过模型验证的情况下屏蔽文本输入中输入的字符
- 在jquery中,将文本框验证为数字
- 将动态验证文本添加到自定义jQuery验证规则中
- Jquery函数来验证响应文本
- 在验证之前从输入值中删除文本并提交
- 对具有相同 id 的多个元素进行文本验证
- 如何添加true和false的输入文本验证
- 通过正则表达式进行 javaScript 文本验证
- 文本验证
- 如果某个类为空jquery,则使用输入文本验证表
- 如何创建文本验证框
- JavaScript文本验证不工作与Regex