Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter just persian text in text box using javascript?

Tags:

jquery

I am create small demo for write persian text enter in text box.i am search some links and try this code but this is not enter persian text enter only english text so can you please help me how can do that.and also want to validation on national id.

this is my html text box :

<input type="text" id="txtn" onkeypress="text(this)">

this is my js code :

function text(name)
{
    var name = $(name).val();           
    just_persian(name);
}

 function just_persian(str) {
    var p = /^[\u0600-\u06FF\s]+$/;
    if (!p.test(str)) {
        alert("not format");
    }
}

here i am enter any text always getting alert.

like image 793
coderwill Avatar asked Dec 04 '22 22:12

coderwill


1 Answers

Try this code:

$("#txtn").on('change keyup paste keydown', function(e) {        
    if(just_persian(e.key) === false)
        e.preventDefault();
});


function just_persian(str) {
   var p = /^[\u0600-\u06FF\s]+$/;
   if (!p.test(str)) {
       return false
   }
   return true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtn">

I hope this work :)

like image 102
Yashar Aliabbasi Avatar answered Dec 11 '22 15:12

Yashar Aliabbasi