Test a Field for Valid Characters using REGEX

[FONT=“Georgia”]Hi.

I’m writing a form validation function and would like to verify that a field only contains alphanumeric characters.

How should I structure that statement?

The following seems logical but doesn’t work;[/FONT]

			if(document.getElementById('RegisForm_Username').value == /[^a-zA-Z 0-9]/g)
				{
				alert("I'm sorry, your username must be alphanumeric (consisting of the letters A-Z or numbers 0-9) and cannot have any special characters.");
				return false;
				}

[FONT=“Georgia”]Any ideas?

[/FONT]

Use the match function (part of the String prototype):


if(document.getElementById('RegisForm_Username').value.match(/[^a-zA-Z 0-9]/g)) 

:slight_smile:

[FONT=“Georgia”]ah, That did it.

Thanks!

[/FONT]