1

I have a code that when someone on input with textn id writes english it says invalid format

function text(name)
{
    var name = $(name).val();
    if (name.length > 0) {
        just_persian(name);
    }
}

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

    }
}

how i delete entered english value after get alert("not format"); alert?

2 Answers2

1

Try this

if (!p.test(str)) {
    alert("not format");
  
    const engCharsRegexp = /^[a-z]+$/gi;
    $(str).val(str.replace(engCharsRegexp, '')));
}

So you just set the value as an empty string

JSEvgeny
  • 2,550
  • 1
  • 24
  • 38
1

To make this work you can use replace() along with a slightly tweaked form of your regular expression to remove any non-Persian characters. Also note that I reorganised the logic slightly to make better use of the strings and objects you have access to:

$('#foo').on('input', e => text(e.target));

let is_just_persian = str => /^[\u0600-\u06FF\s]+$/.test(str);
let replace_non_persian = el => $(el).val((i, v) => v.replace(/[^\u0600-\u06FF]/gi, ''));

function text(el) {
  if (el.value) {
    if (!is_just_persian(el.value)) {
      replace_non_persian(el);
      console.log('not format');
    } 
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="foo" />
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339