You have to specify the
+
character first being optional followed by digits only and matching the whole input:
if(!txt.match(/^\+?[0-9]+$/))
Explained in detail:
^
Begin of string
\+
The
+
character (must be escaped with a backslash here)
?
Matches the preceeding zero or one time
[0-9]
Digits only
+
Matches the preceeding one or more times
$
End of string
An additional option might be limiting the number of digits to a minimum and maximum (here min. 6 and max. 16):
if(!txt.match(/^\+?[0-9]{6,16}$/))
I suggest to read about regular expressions. For JavaScript you might start at
RegExp - JavaScript | MDN[
^].