Aimad MAJDOU Aimad MAJDOU - 7 months ago 30
Javascript Question

test if input has only Arabic characters

I'm trying to check if an input text has only Arabic text in it's value.

This is what I tried :

var test = function() {
var isArabic = /^([\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufbc1]|[\ufbd3-\ufd3f]|[\ufd50-\ufd8f]|[\ufd92-\ufdc7]|[\ufe70-\ufefc]|[\ufdf0-\ufdfd])*$/g;

if(isArabic.test($('arabicFirstname').val())){
console.log('is arabic');
}else{
console.log('not arabic');
}
}


But I'm always getting
not arabic
even though I only type Arabic characters.

How can I solve this ?

Answer

You have missed the jQuery selector like .arabicFirstname or #arabicFirstname in the following:

arabicFirstname is Class Name:

if(isArabic.test($('.arabicFirstname').val())){
   console.log('is arabic');
}else{
   console.log('not arabic');
}

arabicFirstname is ID:

if(isArabic.test($('#arabicFirstname').val())){
   console.log('is arabic');
}else{
   console.log('not arabic');
}