Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with setting alert when value in textbox is greater than a certain value

Hello,

How do I prompt the user with a message if for example he/she enters a value greater than +65 and less than -20 ? I am using VB.NET with my ASP.NET application.

Thanks,

Victor
Avatar of javaftper
javaftper
Flag of Afghanistan image

If VariableName > 65 Or VariableName < -20 Then 
MsgBox "Invalid number"

I

Open in new window

With ASP.NET, you need to try something like this:

Dim message As String = "Your Message"

Dim script As String = "<script type='text/javascript'> alert('" + message + "');</script>"

ClientScript.RegisterClientScriptBlock(Me.GetType(), "AlertBox", script)

Open in new window


Alternatively, if you want to show a message on click of a button, you need to include this code in Page Load Event:

btnSave.Attributes["onClick"] = "alert('Hello World');";

Open in new window


where btnSave is the name of the Save button.
Avatar of Victor  Charles

ASKER

Thanks, I don;t follow the last part.

Do you mean add

btnSave.Attributes["onClick"] = "alert('" + message + "');


on page load in order for the alert to appear if the condition is true?

Victor
Avatar of Shaun Kline
If you do not need immediate feedback, you can use the Range Validator. When an attempt is made to submit the page back to the server, the validator kicks in and displays a message to the user.
I tried the following code in my Save button click event but the message does not appear when I enter a value outside the desired range. Any ideas what is wrong?

If FTL.Text > 65 and FTL.Text < -20 then
Dim message As String = "You are outside the allowed range of data entry"
Dim script As String = "<script type='text/javascript'> alert('" + message + "');</script>"
ClientScript.RegisterClientScriptBlock(Me.GetType(), "AlertBox", script)
End if
Thanks,
V.
I also include the code below in the page load event but it did not work.

 ButtonSave.Attributes["onClick"] = "alert('" + message + "')
ASKER CERTIFIED SOLUTION
Avatar of rpkhare
rpkhare
Flag of India 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
Hi,

Is the validation taken place on the client side or on the server? If possible it would be better to validate my entries from the client side.

Thank you.

Victor
Validators work on both the client and server. This enforces validation even if your user has turned off JavaScript in their browser.
Do you mean in my Btnvalidate click event do the following:

If FTL.Text > 65 and FTL.Text < -20 then
ScriptManager.RegisterClientScriptBlock(Page, GetType(WebForm1), "alert", "<script language='text/javascript'>alert('You are outside the allowed range of data entry');</script>", False)
End if

and in my webform1 pageload event add:

BtnValidate.Attributes.Add("onClick()", "alert()")

I don't follow your html code, does the same code need to be added to my aspx page?

Thanks,

Victor
Hi,

Is it possible to put this code in a method and call it from the onclientblur event of the control? would ike to validate the data when the user leaves the control rather then the button click event.

For example:

Sub validatedata()
If FTL.Text > 65 and FTL.Text < -20 then
ScriptManager.RegisterClientScriptBlock(Page, GetType(WebForm1), "alert", "<script language='text/javascript'>alert('You are outside the allowed range of data entry');</script>", False)
End if
End sun

and on my aspx page:


javascrit <runtatserver.

Function validatedata) [

validatedata()
}
</script>

and on the Button control:

OnclientBlur="validatedata"
Validations should not rely on client-side code only. My code suggested above works well with server-side validation.

However, to call server-side method from client-side code, you can refer these links, but it is quite tricky and cumbersome.

How to call serverside function from client side javascript in ASP.Net?

How to call Server Side function from Client Side Code using PageMethods in ASP.NET AJAX