Jump to content

[SOLVED] Force format in text field?


solarisuser

Recommended Posts

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

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)
}

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");
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.