string starts with x or y

JavaScript and client side scripting.

Moderator: General Moderators

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

string starts with x or y

Post by IGGt »

I'm trying to set up some javascript to check a MySQL query before it gets processed. It needs to check that the query starts with either 'SELECT' or 'SHOW', as these are the only two actions permitted. I also want it to check that there is only one ';' and that it is at the end.

I was doing fine, until I tried to add in the OR (SELECT OR SHOW) part. what I have thus far is:

Code: Select all

function sqlcheck(str) {

		var se1="SELECT"
		var se2="SHOW"
		var sc=";"
		var lse1=str.indexOf(se1)
		var lse2=str.indexOf(se2)
		var lstr=str.length
		var lsc=str.indexOf(sc)
		
		if ((str.indexOf(se1)!=0)||(str.indexOf(se2)!=0)){
		   alert("Start with SELECT or SHOW")
		   return false
		}

		if (str.indexOf(sc)!=str.length-1){
		   alert("end with a ;")
		   return false
		}

 		 return true					
	}

function ValidateForm(){
	document.MySQL_Query.query.value = document.MySQL_Query.query.value.toUpperCase();
	
	var query=document.MySQL_Query.query
	
	if ((query.value==null)||(query.value=="")){
		alert("Please Enter a MySQL Query")
		qyery.focus()
		return false
	}
	if (sqlcheck(query.value)==false){
		query.value=""
		query.focus()
		return false
	}
	return true
 }
what can I do to pick up the two options?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: string starts with x or y

Post by kaszu »

I would use regular expression, but that's just me.

Code: Select all

if ((str.indexOf(se1)!=0)||(str.indexOf(se2)!=0)){
//Currently: if string doesn't start with SELECT or string doesn't start with SHOW
//But if it starts with SELECT, then of course it doesn't start with SHOW, so this
//condition always will be true

//It should be AND instead of OR, correct:
if ((str.indexOf(se1)!=0)&&(str.indexOf(se2)!=0)){
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: string starts with x or y

Post by pickle »

You're doing some server-side testing as well right?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: string starts with x or y

Post by IGGt »

kaszu wrote:

Code: Select all

//It should be AND instead of OR, correct:
if ((str.indexOf(se1)!=0)&&(str.indexOf(se2)!=0)){
I think I can see how that makes sense now. And most importantly, it works. cheers. This is my first outing with javascript, so hopefully it'll get better.

(also, the PHP to check the query is coming next, but I figured this would be a good chance to take a look at some javascript - it's only a development system so far, and not being used live).

cheers
Post Reply