Jump to content

using Regular Expressions


b_suneetha
 Share

Recommended Posts

how to validate a text box that takes only alphabets and not numeric using regular expression, i had written the code as follows, it is not taking the numeric but the combination that is aplha numeric it is taking. i need the text box to take only aphabets var a=/\D[a-z]\D/; if (document.asform.author.value.search(a)==-1) { alert("Please enter only letters"); document.asform.author.value=""; document.asform.author.focus(); return false; } else return true;

Link to comment
Share on other sites

Well you could use something like this, but it might be considered as a "overkill" :)

var str = document.asform.author.value;for (i = 0; i < str.length; i++){	if (str.charCodeAt(i) != --- the values of the accepted chars ---)	{		alert("Please enter only letters");		document.asform.author.value="";		document.asform.author.focus();		return false;	}	else 		return true;}

You'd probably write the script in a different way, but the above shows my idea :)

Link to comment
Share on other sites

Try doing a match for anything that isn't a letter:

var regex = /[^a-zA-Z]/g;if(document.asform.value.match(regex)){	alert("Please enter only letters.");	document.asform.author.value="";	document.asform.author.focus();	return false;}else{	return true;}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...