5 Replies - 3079 Views - Last Post: 29 April 2009 - 04:22 PM Rate Topic: -----

#1 hack_the_planet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 17-April 09

Server Side date validation

Posted 29 April 2009 - 09:05 AM

Hey so i have this problem, i am trying to do some server side validation instead of client side....i no its stupid considering it doesn't need to be secure !
So im trying to validate a date field in a form in the format YYYY/MM/DD and the problem is that everything im using WILL NOT WORK here is an example of one of the functions ive tried:


function validateDate(dateString) {
	/**********************************************
	DESCRIPTION: Validates that a string is in a
	  valid date format (YYYY/MM/DD) BUT ITS NOT WORKING RIGHT NOW !!!
	***********************************************/
	  var regExp = /^([0-9]{4})\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])/;
	  var matches = regExp.exec(dateString);
	  
	  if (!matches) {
				  return false;

	  }else{ 
				  return true; 	
	  } 
}






if(validateDate(formValues.lastSignOffDate)){	
								  
	valid = false;  
	//if the date is returned true "invalid" then it gives the valid form false...							  
 }  




i tho0ught this should work. But then again its not :P
if anyone has any snippets or can point out what im doing wrong that would be a huge help. ive been stuck on this for a while.

Is This A Good Question/Topic? 0
  • +

Replies To: Server Side date validation

#2 hack_the_planet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 17-April 09

Re: Server Side date validation

Posted 29 April 2009 - 09:23 AM

Any help ??
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Server Side date validation

Posted 29 April 2009 - 09:26 AM

@hack_the_planet: You're going to have to show some patience. You post the original question a mere 20 minutes ago, and it normally takes longer than that to get a response. We're all volunteers here and have lives, such as school, families, etc. So be a little patient :)
Was This Post Helpful? 0
  • +
  • -

#4 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Re: Server Side date validation

Posted 29 April 2009 - 09:50 AM

Just for further information. What does the form value look like that you are passing to this regular expression. Are you sure it is getting passed as YYYY/MM/DD. I would try a message box displaying what date string actually looks like. I say this because it will let you see what is getting passed to the method.

*Edit*

You may also try this.

java.util.regex.Pattern
Pattern pattern = Pattern.compile('^([0-9]{4})\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01]'));
Matcher match = p.matcher(dateString);
boolean isValidFormat = match.matches();



then just return isValidFormat. Hopefully this will at least help you some what.

This post has been edited by samuraitux: 29 April 2009 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 hack_the_planet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 17-April 09

Re: Server Side date validation

Posted 29 April 2009 - 10:30 AM

Hey thanks for your replies.

It turns out that this actually works, there was just something else in my code after i checked the validation. So if anyone needs to check validation serverside there you go its the first post on here.

Im also working with SURF platform in Alfresco ECM so java.util.regex.Pattern was out of the question. but thanks anyways.

:) :) :)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Server Side date validation

Posted 29 April 2009 - 04:22 PM

This will validate if numeric/alpha but not if date is valid
I would

boolean validateDate(String toTest) {
	if(toTest.length() != 10)
	  return false;
	char[] digit = toTest.toCharArray();
	if(digit[4] != '/')
	  return false;
	if(digit[7] != '/')
	  return false;
	digit[4] = '-';
	digit[7] = '-';
	try {
		java.sql.Date x = java.sql.Date.valueOf(new String(digit));
		return true;
	}
	catch(Exception e) {
	}
	return false;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1