Adding Regexp to Javascript check

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Adding Regexp to Javascript check

Post by IGGt »

Hi Guys,

I am trying to add a regularexpression to my Javascript to check that the form input contains only 6 digit numbers. The Regex I have used in my MySQL Query is:

^[[:digit:]]{6}\$

But I can't seem to get it into Javascript. So far I have:

Code: Select all

function ValidateForm(){
	
	var jobname=document.MySQL_Query.jobname
	var startdate=document.MySQL_Query.startdate
	var enddate=document.MySQL_Query.enddate
	
	if ((jobname.value==null)||(jobname.value=="")){
		alert("Please Enter a Jobname")
		jobname.focus()
		return false
	}	
	if ((startdate.value==null)||(startdate.value=="")){
		alert("Please Enter a Start Date")
		startdate.focus()
		return false
	}
	if ((enddate.value==null)||(enddate.value=="")){
		alert("Please Enter an End Date")
		enddate.focus()
		return false
	}
	return true
 }
And I was looking at adding something along the lines of:

Code: Select all

	if (document.MySQL_Query.jobname.value.match('/^[[:digit:]]{6}\$/')) {
		return true }
                else {
                alert("Please Enter a Valid Jobname")
		jobname.focus()
		return false
	}
But this always returns false. What am I doing wrong?
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Adding Regexp to Javascript check

Post by IGGt »

I think I have cracked it:

Code: Select all

	if (jobname.value.match("\^\\d{6}\$")) {
		return true }
			else {
		alert("Please Enter a Valid Jobname")
		jobname.focus()
		return false
	}
Post Reply