View Full Version : Form Submit / Format / Pop-Up
PCheese
06-13-2001, 08:40 AM
I'm having a bad hair day (having no hair that's quite impressive!)
I can't seem to recall or work out how to do any number of things so if anyone can help I pormise to lavish them with gifts and praise.
1. How do you prevent a user from pressing Return to submit a form? I would like it to perform the same action as tab (and move between the input boxes on my form) seeing as that is the sort of thing my users are used to.
2. Is there any easy way to format the content of a text box to only accept numbers and slashes (for date entry) (i.e. 19/03/78).
3. How do you get a pop up window to be centred on the screen?
All of these bits and pieces are things I have done before but forgotten. Please help me before I decide to go and boil my head!
Oh yes, all done in ASP / Javascript if poss - I don't know PHP / Perl etc.
vituz
06-14-2001, 06:22 AM
hi!
#1:
try to catch keydown, & if key was 'enter' (ascii code %0d) then ...(your actions)
#2:
use regExp
check it:
http://developer.netscape.com/docs/manuals/communicator/jsguide/regexp.htm#1005439
#3:
use this code:(ie-nn4x compatible)
if ( document.all ){winX=document.body.clientWidth; winY=document.body.clientHeight}
else if (document.layers){winX=window.innerWidth; winY=window.innerHeight}
var x=(window.availWidth-winX)/2
var y=(window.availHeight-winY)/2
window.moveTo(x,y)
didnt checkedit myself, but think it's gotta work
good luck!
I've been having a similar problem as #2...
thanks for pointing to regular expressions.
What would be the expression for a date in the form
2001-07-10? I can't seem to get it right.
I also need an expression to test an input string in this form: a1b (letter number letter). Neither letter should be case sensitive.
Also, what would be an EASY way to make sure that 1 date is after another(ie. if user enters a "from" and a "to" date for a search)? Could I remove the hyphens and compare if one is bigger than the other? How could I do that? Would it be easier to use ".split" and ".join"? What would that look like?
Sorry for all the questions, I would appreciate ANY help!
Cheers!
Ian.
Jon Hanlon
07-10-2001, 09:12 PM
Here is a script to validate a date in the form yyyy-mm-dd. (it will also take yyyy-m-d, yyyy.mm.d, yyyy/m/dd etc.)
function validDate(inStr) {
var date_re = /^\s*(\d{4})[/\.\-\s](\d{1,2})[/\.\-\s](\d{1,2})$/
var matchArray = date_re.exec(inStr);
if (!matchArray) return false;
var year = parseInt(matchArray[1],10);
var month = parseInt(matchArray[2],10) - 1; //*******s
var day = parseInt(matchArray[3],10);
if (year < 1900 || year > 2100) return false;
var realDate = new Date(year, month, day);
if (year != realDate.getYear() ||
month != realDate.getMonth() ||
day != realDate.getDate())
return false;
return realDate;
}
It returns either boolean false (if not a valid date), or a Date object representing the date. It works by utilizing the fact that JavaScript will convert rubbish dates for you. For instance whereas new Date(2001, 1, 20) makes a Date of February 20th 2001 [remembering that the stupid *******s who designed the months in Java Dates had them go from 0..11 and not 1..12], new Date(2001, 1, 38) makes a Date of March 10th 2001. By the way, (new Date(2001, 2, 0)).getDate() is a good way of finding out how many days are in February 2001, but I digress.
If you want to compare two Date objects, use the getTime() method. ie. if (date1.getTime() > date2.getTime())
To test for alpha-digit-alpha, use:
var someString = "f8Y"
var re_ada = /[a-z]\d[a-z]/i
var result = re_ada.test(someString);
Thanks for the expressions Jon!
Unfortunately the code doesn't like any year before 2000. The code you provided returns boolean false after...
if (year != realDate.getYear() ||
month != realDate.getMonth() ||
day != realDate.getDate())
return false;
(it fails on the year comparison)
Ideas?!?!
Thanks,
Ian.
Jon Hanlon
07-11-2001, 07:42 PM
Sorry about that.
For years between 1900 and 1999, getYear() returns the (year - 1900) so for 1997 it returns 97. So all you need to do is:
var theYear = realDate.getYear();
if (theYear < 100) theYear += 1900;
There is a new method in IE called getFullYear() which returns the proper year (1987, 2003 etc.)
I'm not sure if Netscape has it.
Thanks man... awesome.
Ian.
whkoh
07-12-2001, 09:40 AM
Originally posted by jonhanlon
Sorry about that.
For years between 1900 and 1999, getYear() returns the (year - 1900) so for 1997 it returns 97. So all you need to do is:
var theYear = realDate.getYear();
if (theYear < 100) theYear += 1900;
There is a new method in IE called getFullYear() which returns the proper year (1987, 2003 etc.)
I'm not sure if Netscape has it. fetFullYear is able to run on any browser with Javascript 1.1
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.