Link to home
Start Free TrialLog in
Avatar of hreinart
hreinart

asked on

inputvalidation: blank fields

hi,

i posted this question before.
now my needs are a bit different and the suggestions made before seem not to work.

question:
any idea how i can check if fields are empty when pressing save on the web. the save button is a picture with a hotspotrectangle incluging a formular.

best would be if a message would popup displaying each field separately which is blanc (but a user definable text, not the fieldname).

e.g. "please enter the amount" if thre field amount is empty.

if the way i go for it isn't good, please also let me know.


thanks
hreinart
Avatar of CRAK
CRAK
Flag of Netherlands image

Use javascript:
(I assume you're using R5....)

In JS HEader:


function Validate(fld, lbl)
{
  frm = document.forms(0)
  f = frm(fld)
  t = v.type
  ValFlag = 0
  if (t.indexOf('select')>=0)
  {
    if (v.selectedIndex<0)
    {
      ValFlag = 1
    }
  }
  else
  {
    if (v.value=='')
    {
      ValFlag=1
    }
  }
  if (ValFlag==1)
  {
    ValLst = ValLst + '\"' + lbl + '\"\n'
    if (ValFocus=='')
    {
      ValFocus = fld
    }
  }
}

function ValidateForm()
{
  ValLst = ''
  ValFocus=''
  // modify as folowing lines needed:
  Validate('Field1', 'Fieldlabel 1')
  Validate('Field2', 'Fieldlabel 2')
  // end modify
  if (ValLst='')
  {
    alert('Please fill-out following fields:\n\n' + ValLst)
    frm=document.forms[0]
    frm[ValFocus].focus()
    return(0)
  }
  else
  {
    // allow submit
    return(1)
  }
}

function DocSubmit()
{
  ValChk=ValidateForm()
  if (ValChk==1)
  {
    document.forms[0].submit()
  }
}


Make the button call the javascript function "DocSubmit()"

Avatar of hreinart
hreinart

ASKER

hi crack,

looks very interesting, thanks sofar.
where is the java script haeder?
should i paste it into the top of the form and mark it as html?

the button function is at the click event?

i'm using v5, but can't program.

i'll be able to try it on monday!


thanks again
hreinart
hi crack,

i played a little bit with it.
found the java header an implemented the code.
the button script is in the click event.

when pressing the button on the web, a runtime error
occurs.
it points me to the beginning of the script to this line:
 t = v.type

???
any idea what happend?

thanks
hreinart
f = frm(fld)
should be:
v = frm(fld)

or:

Every occurrance of "v" should be renamed "f" in that function.

I wrote the code originally in dutch, on another (off-line) machine, where v was short for "veld", which is "field". "f" Seemed to make things easier to understand.... if I had not made that silly typing error!

I had to retype the code anyway; why not translate where possible, I thought....
My apologies!  ;-))
hi crack,

i found exactly one line where i could change it as you described.
attached you find the code as i use it now.

now no error occures but also nothing happens when pressing this button.

maybe the modification for the fileds is done wrong by ?
could you please give me an expample?
e.g. i use the files "description".


thanks
hreinart



// field validation
function Validate(fld, lbl)
{
 frm = document.forms(0)
 v = frm(fld)
 t = v.type
 ValFlag = 0
 if (t.indexOf('select')>=0)
 {
   if (v.selectedIndex<0)
   {
     ValFlag = 1
   }
 }
 else
 {
   if (v.value=='')
   {
     ValFlag=1
   }
 }
 if (ValFlag==1)
 {
   ValLst = ValLst + '\"' + lbl + '\"\n'
   if (ValFocus=='')
   {
     ValFocus = fld
   }
 }
}

function ValidateForm()
{
 ValLst = ''
 ValFocus=''
 // modify as folowing lines needed:
 Validate('Field1', 'description')
 Validate('Field2', 'Fieldlabel 2')
 // end modify
 if (ValLst='')
 {
   alert('Please fill-out following fields:\n\n' + ValLst)
   frm=document.forms[0]
   frm[ValFocus].focus()
   return(0)
 }
 else
 {
   // allow submit
   return(1)
 }
}

function DocSubmit()
{
 ValChk=ValidateForm()
 if (ValChk==1)
 {
   document.forms[0].submit()
 }
}
Two questions:

Does the button calll the DocSubmit()-function?

Are your fields called Field1 and Field2?

In the ValidateForm()-function you should identify the fieldnames and their labels: every field on a separate line. Add additional ones if neccessary.

Example:
Fieldlable: "Please enter your name: "
Fieldname: "Name"

Add/modify a line like:
Validate('Name', 'Your name')

hi crack,

i tried it again, no chance.
here are the answers:
Does the button calll the DocSubmit()-function?
as far as i can see, yes.
inside the click event it runs the java script:
"DocSubmit()"


Are your fields called Field1 and Field2?
no.
names are like: description, amount, month

In the ValidateForm()-function you should identify the fieldnames and their labels: every field on a
separate line. Add additional ones if neccessary.

Example:
Fieldlable: "Please enter your name: "
Fieldname: "Name"

Add/modify a line like:
Validate('Name', 'Your name')



question:
could you give me a complete new script with validating 2 fields named: decription and amount?
this would make it for me more easy to see where i might created an error.

i guess that it's 98% ready, it'll be a little stupid
thing i don't see (because i don't programm).


thanks
hreinart
Got the bug:

Lookup following lines:
// end modify
if (ValLst='')

Add an exclamation mark as follows:
// end modify
if (ValLst!='')

As I said... I had to re-type my original code again...
....98% .... 99%.... 100% completed now, right?   ;-))
hi crack,

i'll test it now.
...but could you give me the examples for the line with the fieldnames.
i'd like to see the 100% correct code which i would past e into your code.


thanks
hreinart
hi crack,

i'll get an error:
obect needed in line 12 which is:

 t = v.type

(of the source code [view source code])

regards
hreinart
hi crack,

please look at this, if it's ok:

// modify as folowing lines needed:
 Validate('description', 'enter description')
 // end modify
 if (ValLst!='')

-------------------------------
i couldn't find anything like this to modify in  the source:
Fieldlable: "Please enter your name: "
Fieldname: "Name"

where in the code should it be written?
is the the correct java script statement?

???
i'm confused, sorry.

hreinart

hi crack,

i found this:

ValLst = ''
 ValFocus=''

should be the fields be included here?
...i'm just guessing, i have no knowledge about java script.


hreinart
The lines ValLst = '' and ValFocus='' are meant to initialize variables. No need to change.

That error could have two caused. In either case a field is not available: you try to read the value of a non-existing field, or there are no fields at all (as in read-mode). Fields (and their labels) are defined at the call of the ValidateForm()-function.
Please be aware that javascript is case-sensitive. If you define a field in Notes as "test" and refer to in in javascript as "Test", in won't work....

The entire code:

// field validation
function Validate(fld, lbl)
{
frm = document.forms(0)
v = frm(fld)
t = v.type
ValFlag = 0
if (t.indexOf('select')>=0)
{
  if (v.selectedIndex<0)
  {
    ValFlag = 1
  }
}
else
{
  if (v.value=='')
  {
    ValFlag=1
  }
}
if (ValFlag==1)
{
  ValLst = ValLst + '\"' + lbl + '\"\n'
  if (ValFocus=='')
  {
    ValFocus = fld
  }
}
}

function ValidateForm()
{
ValLst = ''
ValFocus=''
//
//
// modify as folowing lines needed:
//
Validate('<FieldName1>', '<Field label 1>') // modify fieldname and label as required
Validate('<FieldName2>', '<Field label 2>') // modify fieldname and label as required
//
// Add or remove Validate-lines as indicated above as required.
//
// end modify
//
//
if (ValLst!='')
{
  alert('Please fill-out following fields:\n\n' + ValLst)
  frm=document.forms[0]
  frm[ValFocus].focus()
  return(0)
}
else
{
  // allow submit
  return(1)
}
}

function DocSubmit()
{
ValChk=ValidateForm()
if (ValChk==1)
{
  document.forms[0].submit()
}
}


... and have a submit-button or action call the javascript DocSubmit()-fuction.

Required changes (field name/lable) definitions are to be made in the ValidateForm-function.

I could send you a working demo if you leave your e-mail address....

yes, that'd be very nice.
please send zipped to:

anrufbeantworter1@gmx.de

then i'll see what exaxtly to put in for fieldname & lable.


thanks
hreinart
Why make it so complicate ?
Use the following code, and it will work fines.
It also check to white spaces, because when the user click  one time on the spacebar, he must still give us a blanc field.

Place the following function in the JS Header:

// A utility function that returns true if a string contains only whitespace characters.

function isblank(s) {
for (var i= 0; i < s.length; i ++) {
var c = s.charAt(i);
if (( c != ' ') && (c != '\n') && (c != '\t')) return false;
}
return true;
}

Within the onBlur or wherever you want it to be, check for the field. E.g.,

if (isblank(document.forms[0].Fname.value)) {
alert("You must enter your first name");
}


Greets,
Sloeber
hi sloeber,

code works fine, but....
... i'd like to check all fields on the form.

could you extend?


best regards
hreinart
i'd like to check it by saving the document.
if i only check when the user lefts a field, i can't be sure that he clicked this field at all.
Sloeber,
My code was somewhat more complicated as it had to work on SELECT-fields as well. Don't ask for checkboxes.... I didn't have any on the form  ;-)
It's almost the same
You have a button on your form Submit
with as formula
@Command([FileSave]);
@Command([FileCloseWindow]);

Now, use the OnSubmit event
The onSubmit event occurs in a browser and the Notes client when the FileSave @command executes.To simulate submitting a document in a browser, follow FileSave with the FileClose @command. Return false from the onSubmit event to abort the save operation.

In the Onsubmit event
write
if (isblank(document.forms[0].Fname.value)) {
alert("You must enter your first name");
}
else if (isblank(document.forms[0].Lname.value)) {
alert("You must enter your Last name");
}
................

Greets,
Sloeber
???
sorry i don't understand this.

could you explain it please a bit more?
better, please give ma an expample to check more that 3 fields.


thanks
hreinart
(will read your answer tomorrow)
It's almost the same
You have a button on your form Submit
with as formula
@Command([FileSave]);
@Command([FileCloseWindow]);

Now, use the OnSubmit event
The onSubmit event occurs in a browser and the Notes client when the FileSave @command executes.To simulate submitting a document in a browser, follow FileSave with the FileClose @command. Return false from the onSubmit event to abort the save operation.

In the Onsubmit event
write
if (isblank(document.forms[0].Fname.value)) {
alert("You must enter your first name");
}
else if (isblank(document.forms[0].Lname.value)) {
alert("You must enter your Last name");
}
................

Greets,
Sloeber
???
sorry i don't understand this.

could you explain it please a bit more?
better, please give ma an expample to check more that 3 fields.


thanks
hreinart
(will read your answer tomorrow)
It's almost the same
You have a button on your form Submit
with as formula
@Command([FileSave]);
@Command([FileCloseWindow]);

Now, use the OnSubmit event
The onSubmit event occurs in a browser and the Notes client when the FileSave @command executes.To simulate submitting a document in a browser, follow FileSave with the FileClose @command. Return false from the onSubmit event to abort the save operation.

In the Onsubmit event
write
if (isblank(document.forms[0].Fname.value)) {
alert("You must enter your first name");
}
else if (isblank(document.forms[0].Lname.value)) {
alert("You must enter your Last name");
}
................

Greets,
Sloeber
Sorry for the double postings, but there was something wrong with my browser.
Here's the final solution
It's a mix from Crack suggestions and mine.
Thanks to Crack.

function DocSubmit()
{
ValChk = ValidateForm()
if (ValChk == 1)
{
document.forms[0].submit()
}
}

function ValidateForm()
{
if (isblank(document.forms[0].Fname.value))
{
alert("You must enter your first name");
return(0);
}
else
{
if (isblank(document.forms[0].Lname.value))
 {
alert("You must enter your Last name");
return(0);
}
else
{
return(1);
}
}
}


function isblank(s) {
for (var i= 0; i < s.length; i ++) {
var c = s.charAt(i);
if (( c != ' ') && (c != '\n') && (c != '\t'))
return false;
}
return true;
}


Greets,
Sloeber
Have you received the demo allright?
hi sloeber,

looks nice, i'll try it out.
could you please give me an advise whre to put each function?

can i extend the if command to e.g. 20 fields to be prooved
if blanc?
are all kind if fields prooveable (even checkboxes etc.)?


thanks
hreinart
Just put the functions in the JS Header.
You can check as much fields as you want.
No, I don't think that it works for Checkboxes.
For checkboxes you must use other methods.
document.forms[0].Fname.value is only for normal editable fields.
For checkboxes you must use something as selectedindex or so (there you must verify each possible value, to look of it's selected or not).
Crak used something in his answer.

I can find it out for you, but I've a meeting know and I don't know how long it will takes.

Greets,
Sloeber
hi sloeber,

i tried in the meantime your previous suggestion:
In the Onsubmit event
write
if (isblank(document.forms[0].Fname.value)) {
alert("You must enter your first name");
}
else if (isblank(document.forms[0].Lname.value)) {
alert("You must enter your Last name");
}


generally it works fine, but....
1) after the error message appears, i see just a blanc page with "form processed". the form shouldn't be cleared so that the user can input the needed data

2) the filed category which is a dialog list field is prompted with an error even if one element is selected.
any idea?

i'll test your new suggestion now.


thanks
hreinart
For the both of you: checkboxes

flg = false
d = document.forms[0]
for (i=0; i<d.elements.length; i++)  // cycle through ALL fields
{
  if (d.elements[i].name=='Box')  // take action if the requested field is found
  {
    if (d.elements[i].checked)
    {
      flg=true // set flag if checked (else leave flag unchanged)
    }
  }
}
alert(flg) // true = checked, false = unchecked


Checkboxes constist of different "fields" for each box, all sharing the same name. This comes from a different demo of mine - not compatible with the other code yet, but easy to adapt. My checkbox field here was "Box" (hardcoded).

Enjoy!
hi sloeber,

i tested now the new version from you (the mix with crack).
more than 3 fields are testable, that's fine.
if a field is empty, the form/screen will stay until all needed data is entered.

the submit button now includes the following statement:
DocSubmit()
action="http://160.58.125.114/open.nsf/frame-open?OpenFrameset"

as far as i can judge it, your java script also saves the document if the fields aren't blanc. that's nice.

only the checkbox, dialog list etc. fields are not checked.
if you have a chance to find it out it'd be nice.

i'll be also in a mmeting and might read your answer tomorrow.



thanks sofar
hreinart
hi crack,

i tested your database which you sent me by mail.
thanks a lot.

it's working...but the error i get referres to the kind of field being investigated.
if it is e.g. a textfield or dialog list field, it runs fine. if it is a checkbox or e.g. radiobutton field, i get the error. probably similar to the comment of sloeber.

i tested this out.

one thing i found out is that if rubbish is entered in a date field the form is not saved, but accepted.
no message is upcomming saying "enter a valid input".
i tried to use a input validation formular like:

@If(date< [01.01.1900]; @Success; @Failure("Please enter a valite date."))

but nothing happens by saving the document (just that it's not saved at all because of the entry e.g. "hello").
might be nice to validate this with javascript in the on blur event. in combination of the existing validation it could be perfect
...but i don't know the code...


bets regards
hreinart
That's where the real "fun" starts!
Some servers need D-M-Y notation, others need M/D/Y....
I must admit that a generic piece of JS to validate that as well could be very usefull; inclusing radiobuttons, checkboxes and other field types... including conditional validations of course.
Haven't got anything suitable ready yet. Will work on it, but can't promise any results: I'm getting quite busy again!
ok, i'll wait.

thanks sofar.
hreinart
Still working on it.... but not a lot of progress yet!
Perhaps (if you really need it), this URL can help:

http://www.codestore.net/A55692/store.nsf/area/9BAA0CE3785D789E8625695900334995?OpenDocument

(Doesn't mean I'm giving up yet.... ;-)) )
i'll have a look at it and time to wait for your progress (which was very good sofar!).

thanks
hreinart
ASKER CERTIFIED SOLUTION
Avatar of CRAK
CRAK
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Intersting code, CRAK, but I've got my own form validation script, which is a lot shorter.
I just wrote a seperate function for different field types and then call these from my main validation function.
Anyway, in principle it's the same, so I'll leave the points for you.
hi crack,

welcome back.
i'll need to reinstall this database to implement your suggestion.
where would i get the needed js-library?
would it it possible to send me a copy of your database (+ the lib?) to hr1@onlinehome.de

jerrith,
why don't you publish your version?
will it test all the needed fieldtypes?



thanks
hreinart
It's pretty customized, but I can probably generalize it for you. Remind me tomorrow so I don't forget.
The js-library is the code under the 3rd step.
You could put in on a page, a form or in a document, as long as you don't have domino translate in to HTML again: it already is (well... kind of...).

Jerrith,
I could get it shorter too, but I don't want anyone figuring out how to validate a number or a date...
It's generic as you noticed.
Still open today, if you need more help please add comments, since much time has passed so the Experts
can be advised of any progress you may have made here.

You have 18 open questions, all of which require your attention.  Please check the HELP DESK link on
the left for Member Agreement, Guidelines and the Question/Answer process.  If you need help to split
points, or other special handling of your questions or account, please post a zero point question in
Community Support with details.

Your attention to updating your open questions is not only needed, but appreciated.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
hreinart---->  You've asked 46 questions here, only closed 34.  This is against our Guidelines.  Please see the links below if you have any questions in this regard, or comment here.    I think you'll agree that asking for help, getting feedback and not closing and awarding questions is not fair to those who help you.  

If you need help in splitting question points between experts, or otherwise in terms of special handling, just comment here, please, with details.  Or post a zero point question in Community Support with your request and the question link (QID).   If more is needed, please let the experts know.

https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please return and update/finalize all your open questions.  If not resolved in one week, we will need to take actions.  I will post this in all your open question, and also ask that you refer to this site's Guidelines and the Member Agreement you signed off on when you joined this site.  They are available here, as I said, here:

https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp
https://www.experts-exchange.com/jsp/infoMemberAgreement.jsp


Your responsiveness in finalizing them is not only needed, but appreciated.  They are:

https://www.experts-exchange.com/jsp/qShow.jsp?ta=browsers&qid=20173133
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20181440
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20172093
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20171086
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20162886
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20162115
https://www.experts-exchange.com/jsp/qShow.jsp?ta=virus&qid=20260178
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20242901
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20260175
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20256256
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20262543
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20260179


EXPERTS----> Please provide closing recommendations here, in the event this needs to be resolved by us and these questions are still open after 7 days.  You may want to check the link below, we added many new TOPIC AREAS yesterday:

https://www.experts-exchange.com/jsp/zonesAll.jsp

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
Well, my code appears to work very well as a standard now for my (current and former) collegues....
I'd still like to hear from hreinhart though. It took me a while to find the time and write the code, and once I did hreinhart wrote that he couldn't get to it right away either.
I wouldn't mind waiting another while to get my comment evaluated properly!
That is very generous of you CRAK, thank you for your dedication and hard work on this question.  I'm sure that hreinart appreciates that, and hope he/she returns soon to provide you some good news.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
would be nice to get the example sent in a database to:
hr1@onlinehome.de


thanks
hreinart