简体   繁体   English

如何在div javascript或jQuery中验证文本框

[英]How to validate text boxes with in a div javascript or jQuery

how do I integrate a type of validation in my JavaScript to the idea of If value of var firstName is = to null do NOT append the var sHtml and summaryHtml and change the class to change the textbox border and clear the field 我如何在我的JavaScript中集成一种验证类型,以使var firstName的值等于=为null的想法不要追加var sHtml和summaryHtml并更改类以更改文本框边框并清除字段

rules: 规则:

firstName = must contain at least 1 letter and no more than 15 letters
lastName = must contain at least 1 letter and no more than 15 letters
jobTitle = must contain something other than an option value of "" (in the html option tag)
eSculation = must contain something other than an option value of "" (in the html option tag)
mobilePhone = must contain 9 numbers.  This field has a mask attached: (999) 999-99999
officePhone =  = must contain 9 numbers.  This field has a mask attached: (999) 999-99999
eMail = Must contain the following symbol: an @ sign and a . to represent xxx@xx.xxx

The JavaScript I am using to submit to a table is below: 我用来提交到表格的JavaScript如下:

newRow = 1;
currentRow = -1;

function CompanyContacts() {
var rowID = parseInt(document.getElementById("ContactsRowCount").value, 10);
rowID++;
if (currentRow > 0) {
    saveEdits();
} else {
    var firstName = $("#ccFirst").val();
    var lastName = $("#ccLast").val();
    var jobTitle = $("#ccjTitle").val();
    var eSculation = $("#ccEsculation").val();
    var mobilePhone = $("#ccMobile").val();
    var officePhone = $("#ccOffice").val();
    var eMail = $("#ccEmail").val();
    var sHtml = "<tr id='row" + rowID + "'>" +
        "<td class='tblStyle68wlb' id=\"ccFirst" + rowID + "\">" + firstName + "</td>" +
            "<input type=\"hidden\" value=\"" + firstName + "\" name=\"cFirst" + rowID + "\" />" +
        "<td class='tblStyle68wl' id=\"ccLast" + rowID + "\">" + lastName + "</td>" +
            "<input type=\"hidden\" value=\"" + lastName + "\" name=\"cLast" + rowID + "\" />" +
        "<td class='tblStyle68wlb' id=\"ccjTitle" + rowID + "\">" + jobTitle + "</td>" +
            "<input type=\"hidden\" value=\"" + jobTitle + "\" name=\"cJobTitle" + rowID + "\" />" +
        "<td class='tblStyle68wl' id=\"ccEsculation" + rowID + "\">" + eSculation + "</td>" +
            "<input type=\"hidden\" value=\"" + eSculation + "\" name=\"cContactPoint" + rowID + "\" />" +
        "<td class='tblStyle68wlb' id=\"ccMobile" + rowID + "\">" + mobilePhone + "</td>" +
            "<input type=\"hidden\" value=\"" + mobilePhone + "\" name=\"cMobilePhone" + rowID + "\" />" +
        "<td class='tblStyle68wl' id=\"ccOffice" + rowID + "\">" + officePhone + "</td>" +
            "<input type=\"hidden\" value=\"" + officePhone + "\" name=\"cOfficePhone" + rowID + "\" />" +
        "<td class='tblStyle68wlb' id=\"ccEmail" + rowID + "\">" + eMail + "</td>" +
            "<input type='hidden' value='" + eMail + "' name='cEmail" + rowID + "' />" +
        "<td class='tblStyle68wl' ><button type='button' class='XsmallButtons' onclick='editRow(" + rowID + ")'>Edit</button>" +
        "</td><td class='tblStyle68wlb' ><button type='button' class='XsmallButtons' onclick='deleteRow(" + rowID + ")'>Delete</button>" +
        "</td></tr>";
    var summaryHtml = "<tr id='SummaryRow" + rowID + "'>" +
        "<td id='ccFirst" + rowID + "'>" + firstName + "</td>" +
        "<td id='ccLast" + rowID + "'>" + lastName + "</td>" +
        "<td id='ccjTitle" + rowID + "'>" + jobTitle + "</td>" +
        "<td id='ccEsculation" + rowID + "'>" + eSculation + "</td>" +
        "<td id='ccMobile" + rowID + "'>" + mobilePhone + "</td>" +
        "<td id='ccOffice" + rowID + "'>" + officePhone + "</td>" +
        "<td id='ccEmail" + rowID + "'>" + eMail + "</td>" +
        "</tr>";
    $("#customerContactSubmitedTable").append(sHtml);
    $("#SummaryCCTable").append(summaryHtml);
    newRow++;
    document.getElementById("ContactsRowCount").value = rowID;
    $("#ccFirst,#ccLast,#ccjTitle,#ccEsculation,#ccMobile,#ccOffice,#ccEmail").val("");
}
}
function editRow(rowID) {
$('#ccFirst').val($('#ccFirst' + rowID).html());
$('#ccLast').val($('#ccLast' + rowID).html());
$('#ccjTitle').val($('#ccjTitle' + rowID).html());
$('#ccEsculation').val($('#ccEsculation' + rowID).html());
$('#ccMobile').val($('#ccMobile' + rowID).html());
$('#ccOffice').val($('#ccOffice' + rowID).html());
$('#ccEmail').val($('#ccEmail' + rowID).html());
currentRow = rowID;
}
function saveEdits() {
$('#ccFirst' + currentRow).html($('#ccFirst').val());
$('#ccLast' + currentRow).html($('#ccLast').val());
$('#ccjTitle' + currentRow).html($('#ccjTitle').val());
$('#ccEsculation' + currentRow).html($('#ccEsculation').val());
$('#ccMobile' + currentRow).html($('#ccMobile').val());
$('#ccOffice' + currentRow).html($('#ccOffice').val());
$('#ccEmail' + currentRow).html($('#ccEmail').val());
$("#ccFirst,#ccLast,#ccjTitle,#ccEsculation,#ccMobile,#ccOffice,#ccEmail").val("");
currentRow = -1;
}
function deleteRow(rowID) {
$('#row' + rowID).remove();
$('#SummaryRow' + rowID).remove();
var rowCount = parseInt(document.getElementById("ContactsRowCount").value, 10);
rowCount--;
document.getElementById("ContactsRowCount").value = rowCount;
}
function ccClear() {
$("#ccFirst,#ccLast,#ccjTitle,#ccEsculation,#ccMobile,#ccOffice,#ccEmail").val("");
}

Add a validation="regex here" to the input tags first of all to give them an easy visual notification. 首先,向输入标签添加一个validation="regex here" ,以便给它们一个简单的视觉通知。 Beyond if you want to validate with jQuery, you can check each value and not send out an ajax request if any are invalid, using something like this to verify that ( string in this case ) values are correct $(your_element_here).val().match(your_regex_here) 除了要使用jQuery进行验证之外,您还可以检查每个值,如果不合法则不发送ajax请求,使用类似的方法来验证(在这种情况下为string)值正确$(your_element_here).val().match(your_regex_here)

Perhaps an if ($(#id).val().match(some_verification_regex) == null){ return false } 可能是if ($(#id).val().match(some_verification_regex) == null){ return false }

only letters: /^[Az]+$/ 仅字母: /^[Az]+$/

phone number like you mentioned above: /^\\(\\d{3}\\) \\d{3}-\\d{4}$/ 像您上面提到的电话号码: /^\\(\\d{3}\\) \\d{3}-\\d{4}$/

What i would suggest is a validation jQuery plugin and you can find many of them and choose what suites your needs from bellow: 我建议使用的是验证jQuery插件,您可以找到很多插件,然后从下面选择适合您的需求的套件:

jquery.bassistance jquery.bassistance

ddarrenjQuery-Live-Form-Validation-For-Twitter-Bootstrap ddarrenjQuery-Live-Form-Validation-Twitter-Bootstrap

jzae fferer-jquery-validation jzae fferer-jquery-validation

Or you search on jQuery.com site to get many jquery compatible validation plugins. 或者,您在jQuery.com网站上搜索以获取许多与jquery兼容的验证插件。

But if you don't want to use any plugin then you have to write your own validation code. 但是,如果您不想使用任何插件,则必须编写自己的验证代码。

Email and other fields validation functions: 电子邮件和其他字段验证功能:

function emailValidate(e){
        var p = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return p.test(e);
    }
function validate(val, min, max){
    return (val.length < min || val.length > max?false:true);
}
vfirstName = validate(firstName,1,15);//if between 1 and 15 will return true
vlastName = validate(lastName ,1,15);//if between 1 and 15 will return true
vjobTitle = validate(jobTitle ,1,50);//if between 1 and 15 will return true
veSculation = validate(eSculation ,1,50);//if between 1 and 15 will return true
vmobilePhone = validate(eSculation ,1,50);//if between 1 and 15 will return true
vofficePhone = validate(officePhone,12,12);//because `(999) 999-99999` length is 12
veMail = emailValidate(eMail);//also will return false if wrong email format
if(vfirstName && vlastName && vjobTitle && veSculation && vmobilePhone && vofficePhone && veMail)
    var errors = false;
else
    var errors = true;

Then before appending the generated row you can add some condition like: 然后,在附加生成的行之前,您可以添加一些条件,例如:

if(!errors){
$("#customerContactSubmitedTable").append(sHtml);
//....rest of your code
}else
alert('please correct the fields');//or any other event

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM