Small JavaScript instance (judge whether the user inputs a number, intercept two digits, rounding)

Source: Internet
Author: User

Reference post: http://topic.csdn.net/t/20060907/19/5006672.html

090103 check whether it is a number and greater than zero

  1. <% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "test_default" %>
  2. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head runat = "server">
  5. <Title> check whether the number is greater than zero </title>
  6. <SCRIPT type = "text/JavaScript">
  7. // Judge the function.
  8. Function fchecknumbyid (ID ){
  9. VaR TXT = Document. getelementbyid (ID );
  10. If (! Isnan (txt. Value) & TXT. value> 0) // if it is a number, false is returned. If it is equal to zero, it cannot pass.
  11. Return true;
  12. Else
  13. {
  14. Return false;
  15. }
  16. }
  17. // Call judgment.
  18. Function fcheck (){
  19. If (fchecknumbyid ('tb1 '))
  20. {
  21. // Execute the business
  22. Alert ('verified ');
  23. Return true;
  24. }
  25. Else
  26. {
  27. Alert ('non-digit or less than 0! ');
  28. Return false;
  29. }
  30. }
  31. </SCRIPT>
  32. </Head>
  33. <Body>
  34. <Form ID = "form1" runat = "server">
  35. <Div>
  36. <Asp: textbox id = "tb1" runat = "server"> </ASP: textbox>
  37. <Input type = "button" onclick = "fcheck ();" value = "check"/>
  38. </Div>
  39. </Form>
  40. </Body>
  41. </Html>

Front-end

  1. <% @ Page Language = "C #" autoeventwireup = "true" codefile = "test1.aspx. cs" inherits = "xcgl_test1" %>
  2. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head runat = "server">
  5. <Title> call the client code to determine whether the input is qualified </title>
  6. <SCRIPT type = "text/JavaScript">
  7. Function fcheck (){
  8. VaR TXT = Document. getelementbyid ('tbenter ');
  9. If (txt. value = '')
  10. {
  11. Alert ('cannot be blank! ');
  12. Return false;
  13. }
  14. Else
  15. {
  16. If (isnan (txt. Value) // determines whether it is a non-digit
  17. {
  18. Alert ('cannot be a non-digit! ');
  19. Return false;
  20. }
  21. Else
  22. {
  23. Alert ('prompt, only two digits are retained in decimal multiples. ');
  24. Return true;
  25. }
  26. }
  27. }
  28. </SCRIPT>
  29. </Head>
  30. <Body>
  31. <Form ID = "form1" runat = "server">
  32. <Div>
  33. <Asp: textbox id = "tbenter" runat = "server"> </ASP: textbox>
  34. <Asp: button id = "btncheck" runat = "server" text = "the server code is executed only after the client determines that the server code is successful." onclick = "btncheck_click"
  35. Onclientclick = "Return fcheck ();"/>
  36. </Div>
  37. </Form>
  38. </Body>
  39. </Html>

Background

  1. Using system;
  2. Using system. Data;
  3. Using system. configuration;
  4. Using system. collections;
  5. Using system. Web;
  6. Using system. Web. Security;
  7. Using system. Web. UI;
  8. Using system. Web. UI. webcontrols;
  9. Using system. Web. UI. webcontrols. webparts;
  10. Using system. Web. UI. htmlcontrols;
  11. Public partial class xcgl_test1: system. Web. UI. Page
  12. {
  13. Protected void page_load (Object sender, eventargs E)
  14. {
  15. }
  16. Protected void btncheck_click (Object sender, eventargs E)
  17. {
  18. // Take two decimal places without rounding.
  19. Double D = convert. todouble (tbenter. Text. Trim ());
  20. Double I = (double) (INT) (D x 100)/100;
  21. // Rounding
  22. String STR = D. tostring ("0 .##");
  23. Response. Write (I + "|" + Str );
  24. }
  25. }

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.