프로그래밍을 처음 사용합니다. 양식을 확인하려고합니다. 나는 PHP로 (Codeigniter를 사용하고있다.) 아무 문제없이 잘 동작한다.jquery 정규식 유효성 검사 오류가 발생했습니다.
jquery에 관해서는 문제가 있습니다. 예를 들어 사용자 이름의 경우 입력이 유효하면 ok 아이콘이 표시됩니다. 이를 위해 :
var FormValidation = function() {
var handleValidation2 = function() {
// for more info visit the official plugin documentation:
// http://docs.jquery.com/Plugins/Validation
var form2 = $('#form-val');
var error2 = $('.alert-error', form2);
var success2 = $('.alert-success', form2);
//IMPORTANT: update CKEDITOR textarea with actual content before submit
form2.on('submit', function() {
for(var instanceName in CKEDITOR.instances) {
CKEDITOR.instances[instanceName].updateElement();
}
})
form2.validate({
errorElement: 'span', //default input error message container
errorClass: 'help-inline', // default input error message class
focusInvalid: false, // do not focus the last invalid input
ignore: "",
rules: {
username: {
minlength: 2,
maxlength: 15,
regex: "^[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*$",
required: true
},
name: {
minlength: 2,
maxlength: 50,
// regex: ,
required: true
},
lastname: {
minlength: 2,
maxlength: 50,
required: true
},
gender: {
required: true
},
date_born: {
required: true
},
email: {
required: true,
email: true
},
status: {
required: true
},
member_id: {
minlength: 5,
},
documentType_id: {
required: true
},
documentn: {
required: true,
minlength: 2
},
address_street: {
required: true
},
address_number: {
required: true
},
address_floor: {
required: false
},
zipcode: {
required: true
},
city: {
required: true
},
state: {
required: true
},
country_id: {
required: true
},
phone: {
required: true
},
cellphoneNumber: {
required: true
},
cellCompany: {
required: true
},
cellbrand: {
required: true
},
marital_status: {
required: true
},
sons: {
required: true
},
},
messages: { // custom messages for radio buttons and checkboxes
username: {
required: "Este campo es requerido",
minlength: "El nombre de usuario debe tener como mínimo 2 caracteres",
maxlength: "El nombre de usuario debe tener como máximo 15 caracteres",
},
membership: {
required: "Please select a Membership type"
},
service: {
required: "Please select at least 2 types of Service",
minlength: jQuery.format("Please select at least {0} types of Service")
}
},
errorPlacement: function (error, element) { // render error placement for each input type
if (element.attr("name") == "education") { // for chosen elements, need to insert the error after the chosen container
error.insertAfter("#form_2_education_chzn");
} else if (element.attr("name") == "membership") { // for uniform radio buttons, insert the after the given container
error.addClass("no-left-padding").insertAfter("#form_2_membership_error");
} else if (element.attr("name") == "editor1" || element.attr("name") == "editor2") { // for wysiwyg editors
error.insertAfter($(element.attr('data-error-container')));
} else if (element.attr("name") == "service") { // for uniform checkboxes, insert the after the given container
error.addClass("no-left-padding").insertAfter("#form_2_service_error");
} else {
error.insertAfter(element); // for other inputs, just perform default behavior
}
},
invalidHandler: function (event, validator) { //display error alert on form submit
success2.hide();
error2.show();
App.scrollTo(error2, -200);
},
highlight: function (element) { // hightlight error inputs
$(element)
.closest('.help-inline').removeClass('ok'); // display OK icon
$(element)
.closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
},
unhighlight: function (element) { // revert the change done by hightlight
$(element)
.closest('.control-group').removeClass('error'); // set error class to the control group
},
success: function (label) {
if (label.attr("for") == "service" || label.attr("for") == "membership") { // for checkboxes and radio buttons, no need to show OK icon
label
.closest('.control-group').removeClass('error').addClass('success');
label.remove(); // remove error label here
} else { // display success icon for other inputs
label
.addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
.closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
}
},
submitHandler: function (form) {
success2.show();
error2.hide();
}
});
$('#form-val').select2({
placeholder: "Select an Option",
allowClear: true
});
//apply validation on chosen dropdown value change, this only needed for chosen dropdown integration.
$('.chosen, .chosen-with-diselect', form2).change(function() {
form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
});
//apply validation on select2 dropdown value change, this only needed for chosen dropdown integration.
$('.select2', form2).change(function() {
form2.validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
});
}
return {
main function to initiate the module
init: function() {
handleValidation2();
}
};
}();
이것은 전체 코드이며 아직 변경하지 않았습니다. 문제는 입력이 2 자 미만이거나 15보다 큰 경우 오류가 표시되지만 올바른 입력 인 경우 아이콘 ok가 표시되지 않고 검사 할 때 다음 오류가 표시된다는 것입니다.
캐치되지 않음 TypeError : 메서드를 호출 할 수 없습니다. 정의되지 않은 '호출'
"정규식"아이콘을 삭제하면 확인이 나타납니다. 그러나 스 니펫에서 볼 수 있듯이 해당 필드는 제대로 작동하지 않습니다.
무엇이 문제 일 수 있습니까? thnx 사전에.
'regex'는 기본 제공되는 검증 방법이 아닙니다. – Barmar
'additional-methods.js'를 로딩하면 일치시킬 정규 표현식을 지정할 수있는'pattern' 메쏘드가 있습니다. – Barmar