如何使用角度js限制文本框中的特殊字符严重重音(反引号)

时间:2017-09-26 10:50:46

标签: javascript angularjs angularjs-forms angular-validation

我想验证文本框,它允许使用特殊字符,如[] ^ _,atoz,1到10个字符。使用ng-pattern =" / ^ [a-zA-Z0-9] * $ / "它允许输入,但它不启用提交按钮。但问题是我们不应该允许用户输入特殊字符,尤其是严重重音(`):

PROCSKETCHDIR="~/sketchbook"
PROCSKETCHDIR="${PROCSKETCHDIR/#\~/$HOME}" # expand home dir ~
echo "$PROCSKETCHDIR"
PROCBINPATH="/PATH/TO/processing-3.3.6" # path/location of Processing executable `processing-java`

MYSKETCH="testprocjavapath"
MYSKETCHDIR="$PROCSKETCHDIR/$MYSKETCH"
# reconstruct folder:
rm -rfv "$MYSKETCHDIR"
mkdir -v "$MYSKETCHDIR"

echo "generating $MYSKETCHDIR/$MYSKETCH.pde"
cat > "$MYSKETCHDIR/$MYSKETCH.pde" <<'EOF'

void setup() {
  size(640, 360);  // Size should be the first statement
  MyJavaClass myjc = new MyJavaClass();
  String thefilecontents = myjc.GetPropsFileContent();
  System.out.format("The properties file content is '%s';%n", thefilecontents);
}

EOF

echo "generating $MYSKETCHDIR/myprops.properties"
cat > "$MYSKETCHDIR/myprops.properties" <<'EOF'
teststr=HelloWorld
EOF

echo "generating $MYSKETCHDIR/MyJavaClass.java"
cat > "$MYSKETCHDIR/MyJavaClass.java" <<'EOF'
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;

public class MyJavaClass {

  private static final String PROPERTIES_FILENAME = "myprops.properties";

  /**
   * add a constructor
   */
  public static void MyJavaClass() {
  }

  public static String GetPropsFileContent() {
    String myret = null;
    myret = readgetFileContent(PROPERTIES_FILENAME);
    return myret;
  }

  public static String readgetFileContent(String inFileName) {
    String result = null;
    Properties properties = new Properties();
    try {
      InputStream in = MyJavaClass.class.getResourceAsStream(inFileName);
      properties.load(in);

      ByteArrayOutputStream resultbaos = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      int length;
      while ((length = in.read(buffer)) != -1) {
        resultbaos.write(buffer, 0, length);
      }
      result = resultbaos.toString();
    } catch (IOException e) {
      System.err.println("There was an error reading " + inFileName + ": " + e.getCause()
          + " : " + e.getMessage());
    } catch (Exception e) {
      System.err.println("There was an exception " + inFileName + ": " + e
          + " : " + e.getMessage());
    }
    return result;
  }
}
EOF

# run once:
"$PROCBINPATH"/processing-java --sketch="$MYSKETCHDIR" --run

并且还在使用以下验证:

<input type="text" ng-pattern="/^[a-zA-Z0-9]*$/">

请提前帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用指令

directive('noSpecialChar', function() {
            return {
                require: 'ngModel',
                restrict: 'A',
                link: function(scope, element, attrs, modelCtrl) {
                    modelCtrl.$parsers.push(function (inputValue) {

                            if (inputValue == null)
                                return ''
                            var cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
                            if (cleanInputValue != inputValue) {
                                modelCtrl.$setViewValue(cleanInputValue);
                                modelCtrl.$render();
                            }
                            return cleanInputValue;

                    });
                }
            }
        })

HTML

<input type='text' no-Special-Char />