在表单提交上验证TextBox

时间:2018-06-07 09:14:07

标签: javascript jquery validation jquery-ui

我有文本框,我想用表格提交替换文本框的值% - 使用jquery提交。但是没有得到我怎么做。

这是我的HTML

var doReplace = function(string){
    return string.replace(/%/g, "-");
}
var $usernameX = $('#usernameX');
$("#form").on("submit", function(e) {
$usernameX.val(doReplace($('#username').val()));
e.preventDefault();
});

JS

import java.util.Scanner;

public class TempConversion
{
    public static void main(String []args)
    {
        Scanner scan = new Scanner(System.in);
        String degSymbol = "\u00b0"; // unicode for the 'degree' symbol 

        do {
            try {
                System.out.print("\nEnter temperature or press q to exit: ");
                String input = scan.next();
                if (input.equals("q") || input.equals(" ")) {
                    System.exit(0);
                } else {
                    double temp_input = Double.parseDouble(input);

                    System.out.printf("What unit of temp is %,.2f? " +
                                      "\nCelsius\nFarenheit\nKelvin: ", temp_input);
                    char unit_from = scan.next().toUpperCase().charAt(0);

                    System.out.printf("\nConvert %,.2f%s%c to what unit? " +
                                      "\nCelsius\nFarenheit\nKelvin: ", 
                                      temp_input, degSymbol, unit_from);
                    char unit_to = scan.next().toUpperCase().charAt(0);

                    String convert = Character.toString(unit_from) +
                                     Character.toString(unit_to);

                    switch(convert) {
                    case "CF": //celsius to farenheit
                        double temp_results = (9.0/5.0) * temp_input + 32;
                        System.out.printf("\nRESULT: %,.2f\u00b0%c = %,.2f\u00b0F\n", 
                                          temp_input, unit_from, temp_results);   
                        break;
                    case "FC": // farenheit to celsius
                        temp_results = (5.0/9.0) * (temp_input - 32);
                        System.out.printf("\nRESULT: %,.2f\u00b0%c =     %,.2f\u00b0C\n",
                                          temp_input, unit_from, temp_results);
                        break;
                    case "CK": //celsius to kelvin
                        temp_results = temp_input + 273.15;
                        System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sK\n", temp_input,
                                          degSymbol, unit_from, temp_results, degSymbol);
                        break;
                    case "FK": // farenheit to kelvin
                        temp_results = (temp_input + 459.67) * 5.0/9.0;
                        System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sK\n", temp_input,
                                          degSymbol, unit_from, temp_results, degSymbol);
                        break;
                    case "KF": // kelvin to farenheit
                        temp_results = temp_input * 9.0/5.0 - 459.67;;
                        System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sF\n", temp_input,
                                          degSymbol, unit_from, temp_results, degSymbol);
                        break;
                    case "KC": // kelvin to celsius
                        temp_results = temp_input - 273.15;
                        System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sC\n", temp_input,
                                          degSymbol, unit_from, temp_results, degSymbol);
                        break;
                    default:
                        System.out.println("\nERROR: One or more variables you entered " +
                                           "are invalid. Please try again.");
                        break;
                    } // ends switch
                } // ends else
            } catch (NumberFormatException ignore) {
                System.out.println("Invalid input.");
            }
        } while(true); //ends do
    } // ends main
} // ends class

2 个答案:

答案 0 :(得分:0)

    var myStr = 'replace_underscore_by_minus';
    var newStr = myStr.replace(/_/g, "-");

答案 1 :(得分:0)

你可以这样做。但是你不需要jQuery

<form [formGroup]="form" id="form" (submit)="onSubmit()" class="form-style-9" ngNativeValidate>
  <div class="form-group">
    <div class="col-sm-4">
      <label for="username">User Name</label>
    </div>
    <div class="col-sm-8">
      <input type="text" id="username" class="form-control" placeholder="Please Enter User Name" formControlName="username"
        required="true">

    </div>
    <input type="hidden" id="usernameX" name="username" />
  </div>
  <input type="submit">
</form>

并向component.ts:

添加onSubmit()
public onSubmit(): void {
 this.addDetails();
 const value = this.form.get('username').value.replace(new RegExp('%'), '-');'
 this.form.reset();
}
相关问题