验证文本框名称

Verify Textbox Names

本文关键字:文本 验证      更新时间:2023-09-26

如何验证我的文本框?这是一个创建用来接收信息的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");
}