Jump to content

javascript newbie - alphanumeric restriction


Recommended Posts

I have one field I need to restrict to accept only alphanumeric characters. I don't want to allow any spaces or hyphens etc. I would also like to specify the exclusion of specific characters (like the letter Q). I found this and I think it meets my need but do not know how to apply it.

I am going to be using it in two different situations. One is for license plate numbers. Only want to allow letters and numbers. The other is in VIN numbers where I want the same restrictions but also want to add specific character restrictions, no Q or O (that's an oh, not a zero) are found in VIN numbers.

Please help format it so I can place it in my footer of the datapage. I have tried adding it to my footer and I can;t seem to get it to work. Thanks

function alphanumeric(plate)

{

var letterNumber = /^[0-9a-zA-Z]+$/;

if((plate.value.match(letterNumber))

{

return true;

}

else

{

alert("Only letters or numbers are allowed in this field");

return false;

}

}

Link to comment
Share on other sites

Hi,

Insert following script into the footer of the DataPage.

This is example for the VIN numbers. If you want to allow Q and O letters, just delete |Q|O from the code below.

<script language="JavaScript">
function alphanumeric(v_plate)
{
  if(!(v_plate.value.match(/[^0-9A-Z]|Q|O/))){
  return true;
 }
 else
 {
 alert("Only letters or numbers are allowed in this field");
 return false;
 }
}
v_b = document.getElementById('Submit');
v_b.onclick = function(){
if(alphanumeric(document.getElementById('INSERTYOURFIELDIDHERE'))) 
{
v_b.submit();
}
else
{
return false; 
}
}
</script>
And replace INSERTYOURFIELDIDHERE with correct field id.

Let me know if this helps.

Link to comment
Share on other sites

What did you replace INSERTYOURFIELDIDHERE with? To verify your field id you can use Firebug which is developer add-on for Firefox browser. You can then inspect the element and find the id. In general, form element IDs in Submission forms are "InsertRecordFIELDNAME", replace FIELDNAME with yours. Form elements in Update forms and Details pages (only editable fields. Display Only fields do not have any ID) are "EditRecordFIELDNAME".

Link to comment
Share on other sites

  • 3 weeks later...

One more question.

I have a license plate field as well as a vin field, both with their own needs in the same datapage. I tried adding the script in twice but there appears to be some conflict as i can only get one to work. I even tried changing the function name to no avail. (v_plate to l_plate). What am I doing wrong? Do I need to merge them into one somehow?

Link to comment
Share on other sites

  • 3 weeks later...

Hi,

Try this code:

<script language="JavaScript">
function checkAlpha1(v_plate){
    if(v_plate.value.match(/[^0-9A-Z]|Q|O/)){
        alert("Only capital letters or numbers are allowed in this field");
        return false;
    }
    return true;
}
function checkAlpha2(v_plate){
    if(v_plate.value.match(/[^0-9A-Z]|Q|O/)){
        alert("Only capital letters or numbers are allowed in this field");
        return false;
    }
    return true;
}
document.getElementById('Submit').onclick = function(){
    if(!checkAlpha1(document.getElementById('INSERT_YOUR_FIELD_1_ID_HERE'))) return false;
    if(!checkAlpha2(document.getElementById('INSERT_YOUR_FIELD_2_ID_HERE'))) return false;
}
</script>

And replace INSERT_YOUR_FIELD_1_ID_HERE and INSERT_YOUR_FIELD_2_ID_HERE with your field ids. Also change criterias in the script for fields, as you need.

Let me know if this helps.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...