how can I go to aspx.cs only if js return true?

shilat shay 1 Reputation point
2022-12-16T13:07:08.307+00:00

I did like this in js (i need to check if the credit card details are correct). so I want instead of the return true to send it to aspx.cs

if (submit != "") {

            if (card != "" && validity != "" && digits != "") {  

                if (card.length == 16) {  

                    for (var i = 0; i < card.length; i++) {  
                        if (card[i] >= 0 && card[i] <= 9) {  

                            for (var i = 0; i < validity.length; i++) {  
                                if (validity[i] >= 0 && validity[i] <= 9) {  

                                    for (var i = 0; i < digits.length; i++) {  
                                        if (digits[i] >= 0 && digits[i] <= 9) {  

                                            if (digits.length == 3) {  
                                                return true;  
                                            }  
                                        }  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
867 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,101 Reputation points Microsoft Vendor
    2022-12-22T08:00:09.973+00:00

    Hi @shilat shay ,

    so I want instead of the return true to send it to aspx.cs

    Based on your description, it looks like you want to send this result to the backend in WebForms project using javascript, right? If this is the case, you could use the ajax request with jquery.js to implement this requirement, like this:

    273214-image.png

    And the WebMethod in asp.cs:

       [WebMethod]  
       public static string MethodName(string name)  
       {  
           return "Hello " + name;  
       }  
    

    In addition, you need to set RedirectMode.Off in RouteConfig.cs. Something like this:

    273208-image.png

    If I misunderstood anything on this question, please let me know.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.