uk sort code regex with string -
i have string input uk sort code. (ex: "my uk sort code 873492"). customer types , agent has respond "my uk sort code ******".
forum posts here regex doesn't how mask uk code , leave string is. please help!
thanks!
as sort codes have 6 digits , formatted 3 pairs of numbers (e.g. 12-34-56
) try match that, need like:
var sortcode = document.getelementbyid("sortcode"); var result = document.getelementbyid("result"); sortcode.oninput = function(e){ var match = sortcode.value.match(/\d{6}|\d{2}\s*?\-\s*?\d{2}\s*?\-\s*?\d{2}/); if(match == null) result.innerhtml = "there no sort code in input"; else result.innerhtml = sortcode.value.replace(/\d{6}|\d{2}\s*?\-\s*?\d{2}\s*?\-\s*?\d{2}/g, "******") };
<input type="text" id="sortcode" placeholder="enter sentence sort code"/> <div id="result"></div>
note match:
- any formatted sort code if there spaces between digits , dashes (e.g.
12- 34 -56
******
). - numbers multiple of 6 number of digits (e.g.
123456789012
************
). - any 6 digits number if not space delimited (e.g.
123456789
******789
).
if don't want that, can make assumptions make more restrictive less probable hide not sort code, @ same time more probable not match sort code not match criteria.
for example, let's sort codes preceded space , @ end of sentence (i.e. followed nothing or dot). code be:
var sortcode = document.getelementbyid("sortcode"); var result = document.getelementbyid("result"); sortcode.oninput = function(e){ var match = sortcode.value.match(/\s\d{6}\.?$|\s\d{2}\s*?\-\s*?\d{2}\s*?\-\s*?\d{2}\.?$/); if(match == null) result.innerhtml = "there no sort code in input"; else result.innerhtml = sortcode.value.replace(/\s\d{6}\.?$|\s\d{2}\s*?\-\s*?\d{2}\s*?\-\s*?\d{2}\.?$/, " ******") };
<input type="text" id="sortcode" placeholder="enter sentence sort code"/> <div id="result"></div>
Comments
Post a Comment