警报后删除字段输入的文本

时间:2020-07-28 14:13:03

标签: javascript html jquery

我有一个代码,当有人输入具有textn id的文字写英语时,它说格式无效

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");

    }
}

如何在收到警报后删除输入的英文值(“ not format”);警报?

2 个答案:

答案 0 :(得分:1)

尝试一下

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

因此,您只需将值设置为空字符串

答案 1 :(得分:1)

要执行此操作,可以使用$('#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" />
W/Glide: Load failed for
http://aplikasi.muanetoraya.com/jaih/uploads/null with size [240x240]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
    There was 1 cause:
    java.io.FileNotFoundException(http://aplikasi.muanetoraya.com/jaih/uploads/null)
     call GlideException#logRootCauses(String) for more detail
      Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed,
class java.io.InputStream, REMOTE
    There was 1 cause:
    java.io.FileNotFoundException(http://aplikasi.muanetoraya.com/jaih/uploads/null)
     call GlideException#logRootCauses(String) for more detail
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
    There was 1 cause:
    java.io.FileNotFoundException(http://aplikasi.muanetoraya.com/jaih/uploads/null)
     call GlideException#logRootCauses(String) for more detail
          Cause (1 of 1): class java.io.FileNotFoundException: http://aplikasi.muanetoraya.com/jaih/uploads/null I/Glide: Root cause (1 of 1)
    java.io.FileNotFoundException: http://aplikasi.muanetoraya.com/jaih/uploads/null