JavaScript 'alert' function without saving form

Hi,I am trying to evaluate what the user enters into my form, on the web.

Scenario.

Access is based on Anonymous entry (browser doesn’t know who’s using it). Form is simply designed so people can send questions in, and questions are essentially emails to a mailbox, taking into account what the user puts in on the web. The form has a SaveOptions of “0” - because theres no need to save these as seperate documents in the database - they are just created as emails.

I have been asked to ‘validate’ a field (for arguments sake, lets say its supposed to contain an employee number and the fieldname is infoName).

I have modified the exisiting ‘contact’ agent - I’m hoping to make it display a message AND not send the email if the employee number is blank…

So far, I’ve tried 1) Two databases from SandBox, 2) Extensive searches of 5 and 6 Forums, 3) Using the onBlur (prev onExit) event of the field, 4) A seperate JavaScript agent, 5) Using messagebox (which just appends a message to log.nsf.

Surely it can’t be this hard just to validate if one field is blank or not?

Heres my current code:-

Sub Initialize

Dim db As Notesdatabase

Dim session As New notessession

Dim recipients( 1 To 1 ) As String

Dim rtitem As Variant

Dim doc As Notesdocument

Dim mdoc As Notesdocument

Dim Continue As Variant



Set db = session.CurrentDatabase

Set doc = session.documentContext



'new stuff



Dim Result1 As Variant

'result1 gets the infoName field in the backend

'The next few lines work out the length of the field

result1 = doc.infoName(0)

Dim x1 As Variant

x = Len(result1)

x1=Cstr(x)

If doc.infoName(0)="" Or  x1 ><8 Then

	Print "<SCRIPT Language=Javascript> alert('Error Msg here')</SCRIPT>"

Else

	

'end of new stuff

	s=doc.contactSubject(0)

	recipients(1) =  doc.contactName(0)

	Set mdoc = db.CreateDocument

	mdoc.form = "Memo"

	mdoc.Subject = "The Source Feedback"

	iFrom=doc.infoName(0)

	

	mdoc.ReplyTo = iFrom

	mdoc.Principal = iFrom

	Set rtitem = New NotesRichTextItem(mdoc,"Body") 

	Call rtitem.AppendText("A Contact Us has been submitted by "+iFrom+"."+Chr$(13) + Chr$(13) + Chr$(13))

	

	If s<>"" Then

		Call rtitem.AppendText("Bulletin: "+s+Chr$(13)+Chr$(13))

	End If

	

	Call rtitem.AppendText(doc.infoComments  + Chr$(13))

	Call mdoc.Send( True,Recipients )

End If		

End Sub

Subject: JavaScript ‘alert’ function without saving form

Have your save button call a function which does the evaluation and then calls submit(); or executes alert(); based on the results.You don’t need an agent and you don’t have to fuss with SaveOptions.

In the button.click event …

if( document.forms[0].field.value == “” )

alert( "fill in the field" );

else

document.forms[0].submit();

Collin

KC8TKA

Subject: RE: JavaScript ‘alert’ function without saving form

Thanks Colin. The problem with this is that my button already executes functions, using formula…Heres what the button executes:-

@Command([ToolsRunMacro];“contact”);

@URLOpen(“/”+@ReplaceSubstring(@Subset(@DbName; -1);“\”;“/”)+“/web01/default?OpenDocument&v=home”)

Can we build that into JavaScript ?

(wouldn’t it be a good idea for someone to set up something on the web that could translate code formula - ls - javascript - etc :slight_smile:

Subject: RE: JavaScript ‘alert’ function without saving form

There is no translation for most of it, so don’t go looking. However, even @Command([ToolsRunMacro]) submits the web form – and the validation method I gave you will prevent that. All formula actions call a function called _doClick (when the database uses JavaScript to generate forms), and _doClick() always checks for the return of whatever code is called in the onsubmit event of the HTML form.

Subject: RE: JavaScript ‘alert’ function without saving form

Stan,Theres many things I don’t understand about this, particulary that this ‘onSubmit’ event works even though the document supposedly isn’t saved…

But who cares!?! It works great and is nice and easy to understand for people who don’t know javascript! Hooray and thanks very much!!

Subject: RE: JavaScript ‘alert’ function without saving form

Andrew, the thing to keep in mind with JavaScript is that it is entirely client-side code. So onSubmit, in this case, is the event that occurs just before the browser sends a form-based request to the server – it really doesn’t care what happens after the request is made.

Domino’s _doClick() function (you can look at it if you view the HTML source for the form) runs the onsubmit code on the form, looking for a true/false value. If the value is true, the form’s submit() method is called; if it’s false, the submit() never happens.

Subject: RE: JavaScript ‘alert’ function without saving form

Stan’s code is good as well.If you want to do the @Command([FileSave]) code in a button place the code in a button but do not hide it.

Using the HTML tab of the button properties…

Instead, in the style, put “display: none”. That will make it hidden but still generated.

Next, at the top of that tab give the button a name.

To execute the button from JavaScript, use the button name and “click” it.

ButtonName.click() will execute your formula button from JavaScript.

Collin

Subject: RE: JavaScript ‘alert’ function without saving form

Use the form’s onsubmit event to call the validation code – your existing Save & Close formula action button will fire the onsubmit event. The JS alert, then, goes into your JS Header and becomes:

function validate() {

if( document.forms[0].field.value == “” ) {

alert( “fill in the field” );

return false;

}

else {

return true;

}

}

The form’s onsubmit event (make sure it’s the web code section) is this:

return validate();

With JS validation, the form will not be submitted at all unless the field passes validation. (And if the user has JS turned off, they can’t run the formula language button.)