Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

正規表現を用いた入力値のバリデーションについて

Kentaro Numata
Tera Guru

はじめまして。

 

現在、Client Scriptで正規表現を用いた入力値のバリデーションを実装しようとしています。

試行錯誤したのですがどうしても意図した動作をしないため、ご相談したく投稿致しました。

バリデーションで満たしたいことは入力値が「半角の数字と大英文字を含む」ことです。(100と100gbはNG、100GBはOK)


下記に作成したスクリプトを記載します。

原因がお分かりになりましたらご教示頂けると幸いです。

 

宜しくお願い致します。

var re = /^[0-9][A-Z]*$/;
     if (!newValue.match(re)){
     getMessage("Please enter the storage 1 capacity in uppercase letters and single-byte numbers.(ex 100GB)", function(msg){alert(msg);});
     g_form.clearValue('u_storage1_capacity');
}

 

※ ClientScriptの発動条件はu_storage1_capacityが更新された時です。

※ newValueはユーザーが入力したu_storage1_capacityが入ります。

1 件の受理された解決策

Hi @Kentaro Numata 

Update the regex like this: 

var pattern = new RegExp(/^[0-9]{3}[A-Z]{2}?$/);

Thanks,
Murthy

元の投稿で解決策を見る

4件の返信4

Murthy Ch
Giga Sage

Hi @Kentaro Numata 

以下の更新されたスクリプトを試してみてください。

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var pattern = new RegExp(/^[0-9A-Z]+$/);
    if (!pattern.test(newValue)) {
        alert(getMessage("Please enter the storage 1 capacity in uppercase letters and single-byte numbers." + "(ex 100GB)"));
        g_form.clearValue("u_storage1_capacity");
    }
}

 

 

 

Thanks,
Murthy

Hello Murthy.

Thank you for replying.

That's very helpful.

 

Updated script.
However, it allows "100".

I want to create a script that does not allow "100" or "100gb" but only allows "100GB".
But that is difficult.

 

Thanks,

Kentaro

Hi @Kentaro Numata 

Update the regex like this: 

var pattern = new RegExp(/^[0-9]{3}[A-Z]{2}?$/);

Thanks,
Murthy

Hello Murthy.

Thank you for replying.

Problem solved!