validation onclick event

Posted by Sravan under JavaScript on 2/11/2010 | Views : 5093 | Status : [Member] | Replies : 5
hi...


iam using valiation form using onclick event . but it here did not send to the the form to the server........

how to call the submit method in onclick event

please send give me any suggetion and code like name phone number, and address alert validation using onclick event




Responses

Posted by: Nishithraj on: 2/11/2010 [Member] Bronze

Up
0
Down
I think the question is repetitious.

You can do that through the validation controls. Use RegularExpressionValidator control for phone number validation.

\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}( x\d{0,})?


is a reg ex for validating U.S phone numbers you can modify as you require

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sravan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/11/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day Sravan

Someone ask the same question today, infact i thought it was you. But its Simple if you want to validate your controls when you click the Button, its simple you can add something like

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if(txtname.text != " " && txtPhone.text !="" && txtaddress.text !="")
{
//Do this
}
else
{
response.write("<script> alert('Please Enter all Details Before you continue');</script>");
}

}


but this is on the server side. I suggest you do this on the client site without writting a single line of code. You can use Validator controls. i have Just Written an Article now dedicated to your problem. a lot of people are asking this question daily , i think its time we point you to the article that has answers for you.

http://www.dotnetfunda.com/articles/article785-how-to-use-validation-controls-in-aspnet-.aspx

The article will be available in few minutes i just need to approve it.


Thank you for posting at dotnetfunda

Vuyiswa Maseko


Thank you for posting at Dotnetfunda
[Administrator]

Sravan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/11/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
It is better to use Validators to make your life easier.
But if you dont like to use them, you might go by manually creating some javascript validations that might invoke before you submit the page.

Say you create a button which might call a javascript validation just before your page submits.

<script type="text/javascript" >

function validation()
{
var elem = document.getElementById('<%= this.txtBox.ClientID%>');
if(elem.value.length > 0)
return false;
// Put all valdations here
return true;
}

</script>


Now from the button click:
<asp:Button runat="server" OnClientClick="javascript:return validation();" OnClick="submit_page" />


So before the page is posted back, the validation block fires and if the function returns true, the page is posted back, otherwise it errors out.

Hope you are clear.

www.abhisheksur.com

Sravan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 2/11/2010 [Member] Bronze

Up
0
Down
You can get the Regular Expression class by:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx

And Different Regular Expressions:
http://www.regexlib.com


Sravan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Elizas on: 6/10/2010 [Member] Starter

Up
0
Down
We can validate textbox by onkeypress event of the control By checking whether the keycode of the key pressed as the user types falls within the range of the number keys 48-57(i.e 0-9 and '.') ,if not belong to that range it will return false then it will disable the keypress action
function NumericValidation(eventObj)
{
var keycode;

if(eventObj.keyCode) //For IE
keycode = eventObj.keyCode;
else if(eventObj.Which)
keycode = eventObj.Which; // For FireFox
else
keycode = eventObj.charCode; // Other Browser

if (keycode!=8) //if the key is the backspace key
{
if (keycode<48||keycode>57) //if not a number
return false; // disable key press
else
return true; // enable key press
}
}
http://www.mindfiresolutions.com/Numeric-validation-using-Javascript-612.php

Sravan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response