6 Replies - 448 Views - Last Post: 08 June 2012 - 09:00 AM

#1 beetlejuice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Dashes and Javascript make me sad.

Posted 08 June 2012 - 05:48 AM

Heya ! here my question : I don't why but when i valide a string ex:12as------------- my string lenght should be 17 , but i don't know why it keep telling me that im under 17 char. Do somebody have an idea ? I keep searching for my mistake.
Here the code
// Check SerialNum
	var sn = trimStr(document.forms[0].VEHSN.value) + '';
	var count_lettre = countLettre(sn) * 1;
	var count_chiffre = countChiffre(sn) * 1;
	var regexp = /^[a-zA-Z0-9-_]+$/;
	if (sn.search(regexp)==-1)
	{
	alert("String invalide");
	}
	else
	{
	alert("OKAY");
	}
	
	//if keep coming in this if even with a string with 17 charactere
if ( sn.length < 17 ) {
		alert("17 caractères sont requis ");
		
		
		return;
	} 


Is This A Good Question/Topic? 0
  • +

Replies To: Dashes and Javascript make me sad.

#2 beetlejuice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Dashes and Javascript make me sad.

Posted 08 June 2012 - 05:54 AM

sorry double post , but i mean .length not lenght
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Dashes and Javascript make me sad.

Posted 08 June 2012 - 06:48 AM

Well, what does trimStr do to the value?

Solution: use FireBug or another Javascript debugger to step into the code to see what's happening.
Was This Post Helpful? 1
  • +
  • -

#4 beetlejuice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Dashes and Javascript make me sad.

Posted 08 June 2012 - 06:56 AM

trimstr:
function trimStr(strng)
	{
	var i = 0;
	var st2 = "";
	for (i = 0; i < strng.length; i++)
	{
		var c=strng.charAt(i);
		if ( c >= "0" && c <= "9" ) 
			st2 = st2 + c;	
		if ( c >= "a" && c <= "z" )
			st2 = st2 + c;
		if ( c >= "A" && c <= "Z" )
			st2 = st2 + c;
	}
	return st2;
}

My main probleme is that it doesn't count anykind of special character :$
Was This Post Helpful? 0
  • +
  • -

#5 beetlejuice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Dashes and Javascript make me sad.

Posted 08 June 2012 - 08:25 AM

Oh crap silly me !!! probleme solve.... i'm bit lunatic i was trimming my '-' myself for nothing... shame on me hahah
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Dashes and Javascript make me sad.

Posted 08 June 2012 - 08:57 AM

Exactly what I figured.

Again though, if you're going to do Javascript development, get yourself a debugger as noted above and learn to use it.
Was This Post Helpful? 1
  • +
  • -

#7 beetlejuice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Dashes and Javascript make me sad.

Posted 08 June 2012 - 09:00 AM

Yeah Thx Jack , i took firedebug it's pretty cool! and well im in an intership so i'm learning different language. I was in BBx and now JS, it'S very different ^^ but thank you hehe
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1