solarisuser Posted July 17, 2007 Share Posted July 17, 2007 Hi All, I'm asking for someone to enter a mac address, and it has to be in the format NN-NN-NN-NN-NN-NN They are entering the data in an <input type=text> field, and I was wondering if JS is the right solution to enforce this input as the user types it in (client-side validation). I'm doing client-side validation but I also would like client-side. Thanks Link to comment https://forums.phpfreaks.com/topic/60434-solved-force-format-in-text-field/ Share on other sites More sharing options...
roopurt18 Posted July 17, 2007 Share Posted July 17, 2007 You can do this with Javascript and a regular expression. function validateMAC(){ var field = document.getElementById("mac_fld_id"); var regexp = /([0-9]{2}-){5}[0-9]{2}/; // Now check field.value against the regexp, which I can't remember how to do // off the top of my head. I think it's regexp.test(field.value) } Link to comment https://forums.phpfreaks.com/topic/60434-solved-force-format-in-text-field/#findComment-300635 Share on other sites More sharing options...
paul2463 Posted July 17, 2007 Share Posted July 17, 2007 I think this is the way function validateMAC(){ var field = document.getElementById("mac_fld_id"); var regex = /([0-9]{2}-){5}[0-9]{2}/; // do the comparison, if we have a match if (regex.test(field)) { alert("Matches"); } else { alert("Does not match"); } } Link to comment https://forums.phpfreaks.com/topic/60434-solved-force-format-in-text-field/#findComment-300653 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.