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
Post a Comment