javascript - Regular expression syntax difficulties -


I'm trying to learn regular expression testing in javascript but I'm hard to imagine. In my example given below, I am trying to test the user input for the first name to ensure that it does not have:

  1. Number
  2. More than 1 free space ('Sarah Jane' is OK, but not 'Sarah Jane') < / Ol>

    This is my job till now, especially any explicit syntax errors, especially reg expression Stand out in this system? Thank you.

      & lt; Script language = "javascript" type = "text / javascript" & gt; Validate the function name () {var input = document.forms ['myForm'] ['fName']. Values; Var re = / \ d \ s {2,} \ w /; If (input == "") {// Make sure some documents are filled. GetElementById ('fNameError'). Style.color = "red"; Document.getElementById ('fNameError'). InnerHTML = "** You must enter a name"; } And if (re-test) {document.getElementById ('fNameError'). Style.color = "red"; Document.getElementById ('fNameError'). InnerHTML = "** names should not contain more than one location between az or A-Z letters and names"; }} & Lt; / Script & gt;    

    Depending on your description, this regex will look better to you:

      var re = / ^ [az] + ([az] +)? $ / I; Inverted as   

    and then re.test :

      else if (! Re.test (input)) { Document .getElementById ('fNameError'). Style.color = "red"; Document.getElementById ('fNameError'). InnerHTML = "** names should not contain more than one location between az or A-Z letters and names"; }    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -