Dynamic "match" / regex?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Good Man

    Dynamic "match" / regex?

    Hi there

    Ideally, I'd like to create one javascript function and pass the file
    extension i'm looking for to see if its there:

    <input type="file" onchange="check File('pdf',this )" />

    and then have the function something like (w/ Prototype)


    function checkFile(theEx tension,theFiel d) {
    var field = $(theField);
    var fieldName = field.name;
    var fieldID = field.id;
    var fieldValue = field.value;
    var matchPattern = "/\."+theExtensio n+"$/im";
    if (fieldValue.mat ch(matchPattern )) {
    alert('Its a '+theExtension+ '!');
    } else {
    alert('no!');
    }
    }


    The problem is, the line with "fieldValue.mat ch" is not using the "var
    matchPattern" value (my RegEx expression)...

    Can anyone make this work / must i use 'eval' to get the match to work
    against the dynamic var I created?

  • Good Man

    #2
    Re: Dynamic &quot;match&quo t; / regex?

    Good Man <heyho@letsgo.c omwrote in news:Xns9962A25 2BF8F8sonicyout h@
    216.196.97.131:
    Hi there
    >
    Ideally, I'd like to create one javascript function and pass the file
    extension i'm looking for to see if its there:
    >
    <input type="file" onchange="check File('pdf',this )" />

    yes, the form field is properly made in real life:

    <input type="file" id="myFile" name="myFile" onchange="check File
    ('pdf',this)" />


    Comment

    • RobG

      #3
      Re: Dynamic &quot;match&quo t; / regex?

      On Jul 4, 5:56 am, Good Man <h...@letsgo.co mwrote:
      Hi there
      >
      Ideally, I'd like to create one javascript function and pass the file
      extension i'm looking for to see if its there:
      >
      <input type="file" onchange="check File('pdf',this )" />
      >
      and then have the function something like (w/ Prototype)
      >
      function checkFile(theEx tension,theFiel d) {
      var field = $(theField);
      var fieldName = field.name;
      var fieldID = field.id;
      var fieldValue = field.value;
      var matchPattern = "/\."+theExtensio n+"$/im";
      Try:

      var matchPattern = new RegExp('\\.' + theExtension + '$','im');

      When constructing a regular expression this way you must quote
      backslashes, hence \\, so \b becomes \\b, \n becomes \\n, etc.
      if (fieldValue.mat ch(matchPattern )) {
      If all you are doing is matching the RegExp, use test:

      if (matchPattern.t est(fieldValue) ) {


      --
      Rob

      Comment

      • Good Man

        #4
        Re: Dynamic &quot;match&quo t; / regex?

        RobG <rgqld@iinet.ne t.auwrote in news:1183495647 .365626.100600
        @j4g2000prf.goo glegroups.com:
        > var matchPattern = "/\."+theExtensio n+"$/im";
        >
        Try:
        >
        var matchPattern = new RegExp('\\.' + theExtension + '$','im');
        >
        When constructing a regular expression this way you must quote
        backslashes, hence \\, so \b becomes \\b, \n becomes \\n, etc.
        >
        > if (fieldValue.mat ch(matchPattern )) {
        >
        If all you are doing is matching the RegExp, use test:
        >
        if (matchPattern.t est(fieldValue) ) {

        Many thanks RobG...

        Comment

        Working...