Reputation: 31
this jSFiddle example is only accept Arabic chars, there's an alert in case you entered English chars or even numbers/dash
1- I need to make it accept numbers & dash
2- accept numbers or dash in case it's in between the entered value & reject it in case it's at the beginning or end.
for example:
at least I need it to accept numbers & dash, Thanks in advance
HTML code:
<div class="search-bar">
<input type="text" class="name">
<input type="submit" value="submit">
JS code:
$(document).ready(function(e){
$('.search-bar').on('change', '.name', function(e) {
var is_arabic = CheckArabicOnly(this);
});
});
function CheckArabicOnly(field) {
var sNewVal = "";
var sFieldVal = field.value;
for(var i = 0; i < sFieldVal.length; i++) {
var ch = sFieldVal.charAt(i);;
var c = ch.charCodeAt(0);
var dash = "-";
if(c < 1536 || c > 1791) {
alert("Please Enter AR characters");
field.value = '';
return false
}
else {
sNewVal += ch;
}
}
field.value = sNewVal;
return true;
}
Upvotes: 0
Views: 409
Reputation: 52185
Another solution would be something akin to the below:
$(document).ready(function(e){
$('.search-bar').on('change', '.name', function(e) {
var is_arabic = CheckArabicOnly(this);
});
});
function CheckArabicOnly(field) {
var sNewVal = "";
var sFieldVal = field.value;
//If the string starts or ends with dashes or digits, this will match and the function will exit. The middle section will check to see if there are more than two consecutive dashes in the string.
var reg = new RegExp("(^(\d|-))|(-{2,})|((\d|-)$)");
if(reg.test(sFieldVal))
{
alert("Invalid");
return false;
}
for(var i = 0; i < sFieldVal.length; i++) {
var ch = sFieldVal.charAt(i);
var c = ch.charCodeAt(0);
var dash = "-";
//45 is the value obtained when a digit is used, so we add it to the list of exceptions.
if((c != 45) && (c < 1536 || c > 1791)) {
alert("Please Enter AR characters");
field.value = '';
return false
}
else {
sNewVal += ch;
}
}
field.value = sNewVal;
return true;
}
Upvotes: 1
Reputation: 215009
This seems to do what you're looking for
^([\u0600-\u06FF]+)(\d*|-*)([\u0600-\u06FF]+)$
https://regex101.com/r/kG8fF1/1
It allows only dashes or only numbers, if you want to match something like "-7-7-", replace the middle part with [\d-]*
:
^([\u0600-\u06FF]+)([\d-]*)([\u0600-\u06FF]+)$
Upvotes: 1