I am trying to create a regular expression which does not allow any special characters except ,
, .
and they should not come side by side.
For example: STax.sdn,skm
should be accepted whereas SDs,.Hnj
should throw an error message. I have used the below code, however it is accepting ,
and .
side by side which I don't want.
function validateAnnouncementTags(){
var announcementTags = document.getElementById("announcementTags").value;
if (announcementTags.search(/[<>'+\"\/`\\\[\]^={}%;@#!$&*()?:|]/)>-1 ) {
$('#announcementTagsSpecialCharError').addClass('show');
} else {
$('#announcementTagsSpecialCharError').addClass('hide');
$('#announcementTagsSpecialCharError').removeClass('show');
}
}
使用此模式:
/^(?!.*[\.,])/
Based on your comments, I am assuming that you want to accept any letters separated by periods or commas. How about we:
we can use
function validateAnnouncementTags() {
var announcementTags=document.getElementById("announcementTags").value;
if (announcementTags.match(/[a-zA-Z\.,]*/)[0] != annoucementTags
|| announcementTags.search(/[\.,][\.,]/) >= 0
) {
$('#announcementTagsSpecialCharError').addClass('show');
} else {
$('#announcementTagsSpecialCharError').addClass('hide');
$('#announcementTagsSpecialCharError').removeClass('show');
}
}
But, if I may be so bold as to assume more structure to your acceptable syntax:
Then we can use:
function validateAnnouncementTags() {
var announcementTags=document.getElementById("announcementTags").value;
if (announcementTags.match(/([a-z0-9]+[\.,]?)*/)[0] != annoucementTags ) {
$('#announcementTagsSpecialCharError').addClass('show');
} else {
$('#announcementTagsSpecialCharError').addClass('hide');
$('#announcementTagsSpecialCharError').removeClass('show');
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.