0

JavaScript の検証タイプを If value of var firstName is = to null という考え方に統合するにはどうすればよいですか var sHtml と summaryHtml を追加せず、クラスを変更してテキスト ボックスの境界線を変更し、フィールドをクリアします

ルール:

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

テーブルに送信するために使用している 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("");
}
4

2 に答える 2

0

最初に入力タグに a を追加しvalidation="regex here"て、視覚的に簡単に通知できるようにします。jQueryで検証する場合を超えて、各値をチェックし、無効な場合はajaxリクエストを送信せず、このようなものを使用して(この場合は文字列)値が正しいことを確認できます$(your_element_here).val().match(your_regex_here)

おそらくif ($(#id).val().match(some_verification_regex) == null){ return false }

文字のみ:/^[A-z]+$/

上記のような電話番号:/^\(\d{3}\) \d{3}-\d{4}$/

于 2013-08-21T23:18:30.403 に答える