function isGoodEmail() {
var email = document.getElementById("<%=txtComapnyEmail.ClientID%>").value;
if (email == "Email") {
if (window.isValidEmail(email)) {
if (/(aol|gmail|yahoo|hotmail)\.com$/.test(email)) {
alert(' valid email, but not for this site. No free service emails!');
return false;
}
return true;
}
return false;
}
Button code:
<asp:Button runat="server" class="button-orange" Text="Confirm and start my company page"
ID="Companystart" OnClick="CompanystartClick" OnClientClick="isGoodEmail" />
I am calling the above JavaScript from the button, but it is not validating the email. Even if entered gmail it is accepted.
use the following
<asp:Button runat="server" class="button-orange" Text="Confirm and start my company page"
ID="Companystart" OnClick="CompanystartClick" OnClientClick="return isGoodEmail()" />
Your are missing 'return' before your client click function call OnClientClick="return isGoodEmail()"
As you have written return true and false in your java-script so in your function call too there should be return before the function name.
For better understanding of return true and false you can go through this link
What's the effect of adding 'return false' to a click event listener?
---EDIT
function isGoodEmail() {
alert("In the beginning");
var email = document.getElementById("<%=txtComapnyEmail.ClientID%>").value;
alert(email);
if (email == "Email") {
if (window.isValidEmail(email)) {
if (/(aol|gmail|yahoo|hotmail)\.com$/.test(email)) {
alert(' valid email, but not for this site. No free service emails!');
return false;
}
return true;
}
return false;
}
If you put OnClientClick="return isGoodEmail()" it will work it invokes JavaScript code.
but suppose you put "gmail" as text for email, then the if condition
if (email == "Email")
is false so nothing is executed inside or returned
hence it feels like JavaScript is not invoked
try using firebug to debug and break-point js code
Related
The email id is entered into a textbox and the validation for email is applied but there seems to be an error that the whole function is probably not called during execution
<head runat="server">
<title></title>
<script type="text/javascript">
function IsValidUrl()
{
var emailbox = document.getElementById('<%=TextBox4.Text %>');<!--textbox4 is used to receive the email entered by the user-->
var email = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (textbox.value.length > 0)<!--the email field should be non empty-->
{
if (email.test(emailbox.value))
{
return true;
}
else
{
alert("Please enter valid Email");<!--incase of an invalid email-->
return false;
}
}
else
{
alert("please enter text");
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" style="margin-left: 340px" Text="Submit" Width="96px" OnClick="Button1_Click1" OnClientClick="javascript:IsValidUrl();"/>
</form>
</body>``
There seems couple of issue with the javascript's IsValidURL function so please replace it with below code.
function IsValidUrl()
{
var emailbox = document.getElementById('<%=TextBox4.ID %>');
var email = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (emailbox.value.length > 0)
{
if (email.test(emailbox.value))
{
return true;
}
else
{
alert("Please enter valid Email");
return false;
}
}
else
{
alert("please enter text");
return false;
}
}
There are mainly two problems
<%=TextBox4.Text %> here you want id of the textBox but you are fetching value of the textBox.
textbox.value.length you want to check value of the TextBox4 but its variable name is emailbox, not textbox.
So I have just corrected those.
Hope this helps you.
Probably your function is working correctly but the page gets submitted even after the validation so replace below:
OnClientClick="javascript:IsValidUrl();"
with
OnClientClick="return javascript:IsValidUrl();"
Edit
Just figured an error in your JS code if (textbox.value.length > 0) line should be if (emailbox.value.length > 0) so try with replace it.
Another issue I found in document.getElementById('<%=TextBox4.Text %>') which should be document.getElementById('<%=TextBox4.ID %>') and in case of using master pages it should be document.getElementById('<%=TextBox4.ClientID %>') so try replacing it too.
Hope this will help !!
I am using this to check if fields are empty. The problem is, the error message is never thrown when a field is empty, it allows submission. Is it because I am trying to run onclick and onclientclick on the button? This is my syntax
HTML
<asp:Button ID="main1212" runat="server" Text="Check If JS Works"
OnClick="DoSomethingDoNothing_OnClick" OnClientClick="return ValidateData();" />
JS
<script type="text/javascript">
function ValidateData() {
var main1212, dropdownselection, dropdownselection1, field21
main1212 = document.getElementByID("txt313").value;
dropdownselection = document.getElementByID("dropdownlist1").value;
dropdownselection1 = document.getElementByID("dropdownlist11").value;
field21 = document.getElementByID("txt12").value;
if (main1212 == '')
{
alert("Error");
return false;
}
if (dropdownselection == '')
{
alert("Error");
return false;
}
if (dropdownlist1 == '')
{
alert("Error")
return false;
}
if (field21 == '')
{
alert("Error");
return false;
}}
</script>
EDIT
If I open the browser console and press the button that should run my script their are no errors displayed?
It's missing a semi-colon in this line (not sure) of the function ValidateData:
var main1212,dropdownselection,dropdownselection1,field21;
(it's good to avoid extra spaces and also not create variables for this)
And it could be more simple, or like this:
window.onload=function(){document.getElementById('main1212').onclick=function(){ValidateData();};}
I have a textbox and a button, on button's clientClick I call javascript function and there is also server side coding.
Problem is, the page gets post back even if I have return False in the javascript.
Here is my javascript function:
function checkALphaNumericFormat(str) {
//get previous value before editing
var txtUserId = document.getElementById('<%=txtUserId.ClientID%>');
var userId = txtUserId.value;
var patternAlphaNumeric = /^[A-z0-9]+$/gi;
var match = userId.match(patternAlphaNumeric);
//Check Null values
if (txtUserId.value != null && txtUserId.value != "") {
//Check for AlphaNumeric values for User Id
if (match == null) {
alert("Please provide valid AlphaNumeric User Id");
return false ;
}
return false ;
}
else {
alert("User Id field should not be null");
return false ;
}
return false ;
}
and I am calling this function on my Form as:
<asp:Button runat="server" ID="btnCreate" CssClass="loginButton" style="margin:0px 0px 1px 30px;" OnClientClick ="return checkALphaNumericFormat(this.value);" Text="CREATE" />
Try to call JavaScript function as below:
OnClientClick="if(!validateform()){return false;}
where validateform() should be your java script function. Your java script function should have return = true; at the end of function in successfull execution, like in below function:
function validateform()
{
var txtSearch = document.getElementById('<%=txtKeywordSearch.ClientID%>')
if(txtSearch.value == '')
{
alert('No Search Creatria Selected!');
return false;
}
return true;
}
Please try and let me know if it works for you.
Thanks,
Harish
I Have The Following JavaScript Function:
function validateLocality(object, args) {
if (document.getElementById("<%=ddlLocality.ClientID %>").value == "0") {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
and the following drop down list with validator:
<asp:DropDownList
ID="ddlLocality"
runat="server"
DataSourceID="DataSourceListTowns"
DataTextField="town_name"
DataValueField="town_id"
ToolTip="The Locality Where the User Lives"
AppendDataBoundItems="True"
ViewStateMode="Disabled">
<asp:ListItem Value="0">Select Locality</asp:ListItem></asp:DropDownList>
<asp:CustomValidator
runat="server"
ControlToValidate="ddlLocality"
ErrorMessage="Select Locality"
ToolTip="Select Locality"
ID="validateLocality"
ClientValidationFunction="validateLocality()">*</asp:CustomValidator>
The Thing Is It is not validating the drop-down at all.
Thanks For Any Help Cause I wrecked My Brain about this
The ClientValidationFunction should carry only the name of the function. It is not expecting to carrry a Javascript expression. Hence your attribute should look like: ClientValidationFunction="validateLocality" note no ().
Your function is taking 2 arguments:
function validateLocality(object, args)
But when you call it, you aren't passing any in..?
ClientValidationFunction="validateLocality()"
Add an alert to your function to make sure it's being called on the submit.
function validateLocality(object, args)
{
alert("working");
if (document.getElementById("<%=ddlLocality.ClientID %>").value == "0")
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
I have this much
<asp:Button ID="btnSaveContest" runat="server" Text="Save & Publish Contest" OnClientClick="javascript:changeInputTexts(); return disableAfterClick();"
ValidationGroup="ContestAdd" OnClick="btnSaveContest_Click" />
Now I want to call disableAfterClick() after all validations are completed. It should be in Client Side. So that I can ensure that the user can click the button only one time.
function ValidateCreateContest() {
Page_ClientValidate();
if (Page_IsValid) {
$('#<%=btnSaveContest.ClientID%>').attr('disabled', 'disabled');
return true;
}
else {
return false;
}
}`