Changing match to allow null input in javascript -


i have inherited code causing problems in safari.

the problem comes few lines in code things this:

if ( ... && $("#element1").val().match(/regex/) && ...) 

the javascript programmatically generated.

the problem $("#element1").val() returns null , can't put typeof check before it, because needs treat null empty string.

the easiest (and manageable) solution either create nullmatch function , call instead or override .match function itself. new function check null first , (if null) pass empty string match instead of null.

i not sure how either, or best.

it better either replace, or add (e.g.
$("element1").val().nullmatch(/regex/) or $("element1").val().nulltoempty().match(/regex/)

that isn't possible, because .nullmatch or .nulltoempty need method on possibly null value.

if want write in fashion, or it's easier backend generate, write mini-plugin:

$.fn.valnulltoempty = function() { return this.val() || ''; }  $("element1").valnulltoempty().match(/regex/) 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -