aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Set the textfield (optional) in the form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Set the textfield (optional) in the form" Watch "Set the textfield (optional) in the form" New topic
Author

Set the textfield (optional) in the form

Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Eric and everybody
I have a javascript function verify(f) performs form verification.And it will be invoked from the onSubmit() event handler.The function loops through the elements of the form,looking for all text and textarea elements that do not have an "optional" property defined.
Firstly, I do not know how to set the textfield property which is optional that using javascript to check.
<input type="text" name="inputname" size="7" />
And,which function I can use to get the property optional in the javascript

It is not my homework but a passpaper exam question.
I use a lot of the time to search some books, but can not find out the answer.
Hope anybody can help me to solve the problem
thanks for your time and attention


Francis Siu
SCJP, MCDBA
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
look at my form validation utility (my sig), see if that helps
Eric
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Eric
I see the Validation Utility Documentation but can not find out the answer
Let me explain more about the question
The function loops through the elements of the form,looking for all text and textarea elements that do not have an "optional" property defined.
For example,we fill in some information in the form that some details must fill(almost use * point out)but some textfield is optional to fill in.
Could the textfield be possible to define the property as optional,so that the javascript can use it to check?
I mean that using something like textfield.value to get the value.
thanks for your time

Your AI assignment/project seems to be very difficult
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
siu,
here is the code i wrote for ur req
function onError(form_object, input_object, object_value, error_message)
{
alert(error_message);
return false;
}

function hasValue(obj, obj_type)
{
if (obj_type == "TEXT" || obj_type == "PASSWORD")
{
if (obj.value.length == 0)
return false;
else
return true;
}



}
function checkFORM()
{
if (!hasValue(form.FIRST_NAME, "TEXT" ))
{
if (!onError(form, form.FIRST_NAME, form.FIRST_NAME.value, "The First Name field is a required entry and must be completed before the record can be saved."))
{
return false;
}
}

if (!hasValue(form.LAST_NAME, "TEXT" ))
{
if (!onError(form, form.LAST_NAME, form.LAST_NAME.value, "The Last Name field is a required entry and must be completed before the record can be saved."))
{
return false;
}
}



return true;
}
now the form( not posting html tags)
frst two boxes r req and teh third one optional
any probs reply
FORM NAME="form" ACTION="" METHOD="post" onSubmit = "return checkFORM(document.form)"


INPUT type="text" Name="FIRST_NAME"
INPUT TYPE="TEXT" Name="LAST_NAME"
input TYPE ="TEXT" name="BIRTH_DATE"
INPUT TYPE="submit"
NAME="saveButton"
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi sunitha
thanks for your time to answer the question
I may describe it ambiguous
The function loops through the elements of the form,looking for all text and textarea elements that do not have an "optional" property defined.
And using the following coding to check,because the user may add more than three textfields and textareas,so that using following loop can check all these which may contain the property "optional".

I think that textfield may contains some attributes which can use it to check whether it is optional.
Such as the first name that can be empty value that the function can get the first name property to know whether it is optional.
thanks agains
[ May 22, 2003: Message edited by: siu chung man ]
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
siu,
u have some text boxes and u need to validate only those text boxes which are required and u dont need to validate the other text boxes or text areas which are optional( which means user may or may not add no need to force the user). if that is yes my code will work.so frst tell me im a thinking in the rt direction u thinking
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
sunitha
thanks
Yes,your code works
I appreciate I seems to be waste your efforts and times.I am sorry.
The requirement without tell me how many textfields and textareas
If we can find out the property "optional" in textfield,we can write the coding more feasible..
And I am grateful if you can help me to find it out.

[ May 22, 2003: Message edited by: siu chung man ]
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
Originally posted by siu chung man:
sunitha
thanks
Yes,your code works
I appreciate I seems to be waste your efforts and times.I am sorry.
The requirement without tell me how many textfields and textareas
If we can find out the property "optional" in textfield,we can write the coding more feasible..
And I am grateful if you can help me to find it out.

[ May 22, 2003: Message edited by: siu chung man ]

siu,
pls dont say sorry just say i will take u out for dinner( chinese welcome)
The requirement without tell me how many textfields and textareas
can u pls explain this ..im confused...
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
thanks sunitha
pls dont say sorry just say i will take u out for dinner( chinese welcome)
I afraid you are angry because I know you spend a lot of time to write the coding and play the efforts.
The whole question is that
Write a javascript function verify(f) performs form verification.It will be invoked from the onSubmit() event handler.The function loops through the elements of the form,looking for all text and textarea elements that do not have an "optional" property defined. Then check for fields that are empty and make a list of them.If there were any errors,then display the error messages, and return false to prevent the form from being submitted,otherwise return true.

Actually,I get confusion too ,after I read a lot of times,and I use a lot of time to find out answer from the book called "Internet & world wide web how to program".
Finally, I find the book not to program
I wonder if you can help me.
thanks to spend time to read a long question

Server time about 11:45am
but Hong Knog time about 2:15am
I need to sleep,I will read it about eight hours later. thanks
[ May 22, 2003: Message edited by: siu chung man ]
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Id' interpret the part about checking the "optional" property as this:
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
the que is so twisted to confuse u and u still confused
btw wat abt dinner
(u didnt answer that)
//that do not have an "optional" property defined.// this is to confuse u
ie means the req fields
check this
function formCheck(formobj){
//1) Enter name of mandatory fields
var fieldRequired = Array("FirstName", "LastName");
//2) Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name");
//3) Enter dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}
if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
//-->

form name="formcheck" onsubmit="return formCheck(this);"
First Name: input type=text name="FirstName" size="25"
Last Name: input type=text name="LastName" size="25"
input type=submit value="Submit Form"
form
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
all I was showing you with my validation was how to get the attribute of the object by looping through the code without giving you the answer.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
thanks James & sunitha & Eric & everybody help me to find out the answer
The answer were found out by James ,sunitha answer also correct,thanks for your help again and again.
May be my requirement is high ,I find James answer that is more feasible.
Because the answer use the optional property that I think it may be feasible
<textarea name="textarea2" cols="5" rows="1" optional="true" ></textarea>
I see the mentioned book "www how to program" which covered about 1000 pages but without this property.
Eric
I see your Validation Utility Documentation which is about the validation rule for different type of textfield such as check the pattern of ID.And all the textfields are required add something to it to check the pattern. I think it is more complex than my exam. Of course ,it is useful for my year 3 project,I must need it later.
thanks for everybody help~~~~~
 
Consider Paul's rocket mass heater.
 
subject: Set the textfield (optional) in the form