PDA

View Full Version : Verify a number has been entered


Nick Toye
05-25-2004, 05:38 AM
I have a form and I want to check if the data entered is a number if it isnt I want to be able to return an error message. I have the form pretty much nailed apart from this little problem.


// set some variables
//
//
mailform = "mailform.php";
confirm = "thanks";
error1 = "contact name required";
error2 = "company name required";
error3 = "number required";
error4 = "valid email required";
error5 = "input required";
error6 = "weight required";
codeError = "need code";
response.text = "";
//
//
//
//
// validate email function
//
function validate(address) {
if (address.length>=7) {
if (address.indexOf("@")>0) {
if ((address.indexOf("@")+2)<address.lastIndexOf(".")) {
if (address.lastIndexOf(".")<(address.length-2)) {
return (true);
}
}
}
}
return (false);
}
//
// clear form
function clearform() {
username.text = "";
company.text = "";
code.text = "";
telephone.text = "";
email.text = "";
from.text = "";
to.text = "";
weight.text = "";
nextDay_rb.setValue(true);
sameDay_rb.setValue(false);
before9_rb.setValue(false);
before12_rb.setValue(false);
response.text = "";
}
// form check
//
function formcheck() {
if ((((email.text == null)) || (email.text.length<1)) || (email.text == "valid email required")) {
email.text = error4;
formData.action = "";
}
if (!validate(email.text)) {
email.text = error4;
formData.action = "";
}
if ((username.text == null) || (username.text == "")) {
username.text = error1;
formData.action = "";
}
if ((company.text == null) || (company.text == "")) {
company.text = error2;
formData.action = "";
}
if ((code.text == "") || (!isNaN(code.text))) {
code.text = codeError;
formData.action = "";
}
if ((telephone.text == "") || (isNaN(telephone.text))) {
telephone.text = error3;
formData.action = "";
} if ((from.text == null) || (from.text == "")) {
from.text = error5;
formData.action = "";
}
if ((to.text == null) || (to.text == "")) {
to.text = error5;
formData.action = "";
}
if ((weight.text == null) || (isNaN(weight.text))) {
weight.text = error6;
formData.action = "";
}
if ((validate(email.text)) && (email.text != error4) && (username.text != "") && (username.text != error1) && (company.text != "") && (company.text != error2) && (code.text != "") && (code.text != codeError) && (telephone.text != "") && (telephone.text != error3) && (from != "") && (from.text != error5) && (to.text != "") && (to.text != error5) && (weight.text != "") && (weight.text != error6)) {
return (true);
}
}
//new LoadVars function
function sendInfo() {
trace("formcheck() returned true, run actions");
//test trace
formData = new LoadVars();
formData.username = username.text;
formData.company = company.text;
formData.code = code.text;
formData.number = number.text;
formData.email = email.text;
formData.from = from.text;
formData.to = to.text;
formData.weight = weight.text;
formData.delivery = deliveryGroup.getValue();
formData.action = "send";
// make new LoadVars for returned data
returnData = new LoadVars();
// send information
formData.sendAndLoad(mailform, returnData, "POST");
// load the returned data
returnData.onLoad = function(success) {
trace("----------------connected to php page----------------");
if (success) {
response.text = "thanks, a confirmation email has been sent to your email address";
} else {
trace("no returned data");
}
};
//------------test formData info, this will trace before returnData info
trace("formData.action ="+formData.action);
//test trace
trace("formData.name ="+formData.name);
//test trace
}
//end sendInfo()---------
stop();


I have highlighted the area that I need to change, many thanks in advance

Nick

farafiro
05-25-2004, 05:48 AM
u can use 'restrict' to prevent any entries but numbersmy_txt.restrict = "0-9";

Nick Toye
05-25-2004, 05:50 AM
so isNaN has nothing to do with it?

farafiro
05-25-2004, 06:04 AM
nop, the isNaN checks if the text field is empty

Billy T
05-25-2004, 06:04 AM
why do you have a ! in front of the first isNaN?

Billy T
05-25-2004, 06:06 AM
nop, the isNaN checks if the text field is empty

umm not sure if you meant that...did you?

Nick Toye
05-25-2004, 06:09 AM
Billy T, yeah that was a mistake and I took it out. Well the code seems to work, if I put in an alpha character in the weight, code or telephone input box it throws it out and asks for the input again, that is with isNaN used, you can check the form out at www.keepcouriers.co.uk (a little plug there), and see how it works.

Billy T
05-25-2004, 06:13 AM
yep works well

as farafiro said though - it probably would be better to just use 'restrict'

farafiro
05-25-2004, 06:18 AM
nop,I've meant it in such a way
the
myText.text = "" // this may be true if u just hitted the spacebar within the textField
myText.text = !isNaN(myText.text), would return true or false upon the numbers entered

or maybe I'm wrong?? :confused:

Nick Toye
05-25-2004, 06:26 AM
No thats right Farafiro, just checked it by pressing space once, and it allowed the form, how can I adapt my code to prevent space being recognised as an alpha character?

farafiro
05-25-2004, 06:32 AM
I don't think u can do that, but u can check if the text field's content is numbers or not

Nick Toye
05-25-2004, 06:41 AM
Well i've done that with the numbers and that works ok