#jquery #regex #validation
#jquery — запрос #регулярное выражение #валидация #jquery #проверка
Вопрос:
всякий раз, когда я проверяю код, описание, localisedDescription.мой метод validate.addmethod не работает должным образом. не могли бы вы, пожалуйста, дать мне решение как можно скорее, чтобы мое регулярное выражение работало должным образом.Это оператор проверки для кода, описания, localisedDescription.
Fields amp; Actions
1. Code
a. Length - 20
b. Regular Expression – alphabets digit
i. No special characters No spaces No dashes
c. Default value: Blank
d. Mandatory field
e. Validation
i. On Submit (Not On-field tab)
ii. Label Highlighted in Bold Red
iii. Text field – Text highlighted for Invalid data
iv. Validation message on top of the screen
v. For Blank data– Message:- ‘Code is a required field. It must consist of Roman Letters/numbers. Maximum length should not exceed 20 characters. ‘
vi. Invalid data – Message:- ‘Code is a required field. It must consist of Roman Letters/numbers. Maximum length should not exceed 20 characters.
2. Description
a. Length – 80
b. Regular Expression – alphabets digit dashes spaces apostrophe
c. Default value: Blank
d. Mandatory field
e. Validation
i. On Submit (Not On-field tab)
ii. Label Highlighted in Bold Red
iii. Text field – Text highlighted
iv. Validation message on top of the screen
v. Blank – Message:- ‘English Description is a required field. It must consist of Roman letters/numbers, spaces, apostrophes and dashes and Maximum length should not exceed 80 characters.’
vi. Invalid data – Message:- ‘English Description is a required field. It must consist of Roman letters/numbers, spaces, apostrophes and dashes and Maximum length should not exceed 80 characters
3. Arabic Description
a. Length – 80
b. Regular Expression – Arabic characters digits dashes spaces
c. Default value: Blank
d. Mandatory field
e. Validation
i. On Submit (Not On-field tab)
ii. Label Highlighted in Bold Red
iii. Text field – Text highlighted
iv. Validation message on top of the screen
v. Blank – Message:- ‘Arabic Description is a required field. It must consist of Arabic characters/numbers, spaces and dashes and Maximum length should not exceed 80 characters’
vi. Invalid data – Message:- ‘Arabic Description is a required field. It must consist of Arabic characters/numbers, spaces and dashes. Maximum length should not exceed 80 characters’`enter code here`
$.validator.addMethod("adminCodeCheck", function(value) {
alert("hello");
return (/^[A-Za-z0-9]{0,20}$/.test(value));
});
$.validator.addMethod("adminDescriptionCheck", function(value) {
return (/^[a-zA-Z][a-zA-Z0-9 -']{0,80}$/).test(value);
});
$.validator.addMethod("adminArabicDescriptionCheck", function(value) {
return (/^[a-zA-Z][a-zA-Z0-9 -]{0,80}$/).test(value);
});
function validateCarrier(formId, errorcontainerId){
return $(formId).validate({
rules: {
code: {
required: {
depends:function(){
alert("hello2");
$(this).val($.trim($(this).val()));
return true;
}
}
//adminCodeCheck: true
},
description: {
required: {
depends:function(){
$(this).val($.trim($(this).val()));
return true;
}
}
// adminDescriptionCheck: true
},
localisedDescription: {
required: {
depends:function(){
$(this).val($.trim($(this).val()));
return true;
}
}
// adminArabicDescriptionCheck: true
}
},
messages: {
code: {
required: AdminCodeRequired
//adminCodeCheck:AdminCodeRequired
},
description: {
required: AdminDescriptionRequired
//adminDescriptionCheck: AdminDescriptionRequired
},
localisedDescription: {
required: AdminArabicDescriptionRequired
//adminArabicDescriptionCheck:AdminArabicDescriptionRequired
}
},
errorContainer: errorcontainerId,
errorLabelContainer: errorcontainerId "ul",
wrapper: "li"
});
}
function highlightcarrier(validator){
var isError =false;
if(!validator.element("#code")){
$("#code").addClass("errorclass");
isError = true;
} else {
$("#code").removeClass("errorclass");
isError = true;
}
if(!validator.element("#description")){
$("#description").addClass("errorclass");
isError = true;
} else {
$("#description").removeClass("errorclass");
isError = true;
}
if(!validator.element("#localisedDescription")){
$("#localisedDescription").addClass("errorclass");
isError = true;
} else {
$("#localisedDescription").removeClass("errorclass");
isError = true;
}
return isError;
}
Ответ №1:
Для «кода»: /^[A-Za-z0-9]{0,20}$/
Для «описания»: /^[A-Za-z0-9s'-]{0,80}$/
Для «localisedDescription» вам придется использовать плагин. Например, вы можете использовать XRegExp (находится здесь:http://xregexp.com/plugins /) для создания этого шаблона: XRegExp("^[\p{Arabic}s'-]{0,80}$")