Jump to content

need help with a string match


BJOBrien

Recommended Posts

I've got a string of characters that look like

 

Echo procedure 12/20/2016 12:05:32 PM

 

The first word of the line can change. The second word is always procedure then a date and then a time then AM or PM

 

how can I see if a string I have 'matches' this format?

Edited by BJOBrien
Link to comment
Share on other sites

What is the effect of producing a false positive?

var str = 'Echo procedure 12/20/2016 12:05:32 PM';
var offset = str.indexOf('procedure');
var firstblank = str.indexOf(' ');
if(offset != -1 && firstblank + 1 == offset && str[offset + 12] == '/' && str[offset + 15] == '/'){
  alert('match');
}else{
  alert('no match');
}
  • Like 1
Link to comment
Share on other sites

checkstr('');

checkstr(' ');

checkstr('procedure ');

checkstr(' procedure ');

checkstr(' xxxxxxxxxx procedure / /');

checkstr('xxxxxxxxxx procedure / /');

checkstr('Echo procedure 12/20/2016 12:05:32 PM');

 

function checkstr(str){

'use strict';

if (str){ // eliminates null, undefined, and empty string

var offset = str.indexOf('procedure');

var firstblank = str.indexOf(' ');

if(offset != -1 && firstblank + 1 == offset && str[offset + 12] == '/' && str[offset + 15] == '/'){

alert('['+str+']\nmatch');

}else{

alert('['+str+']\nfail');

}

}else{

alert('invalid');

}

}

Link to comment
Share on other sites

RegEx version

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />
        <title>Document Title</title>
        <style type="text/css">
            #resultMessage {padding: 0 15px; color: red;}
            #resultMessage.pass {color: lime;}
        </style>
    </head>
    <body>
        <div>
            <input id="string_date_time" style="width: 300px;" value="Echo procedure 20/12/2016 12:05:32 PM" onchange="validatethis()"></div>

        <script type="text/javascript">
            var t;
            function validatethis() {
                if (document.getElementById('resultMessage')) {

                    document.getElementById('resultMessage').remove();
                }
                var str = document.getElementById('string_date_time');
                var spanElem = document.createElement('span');
                spanElem.id = "resultMessage";

                var offset = str.value.indexOf('procedure ');

                var val_date_time = /(\d{2}\/){2}\d{4}\s+((\d{2})(\){2}\d{2}\s+(AM|PM)/;

                if (str.value.length !== 0) {


                    if (offset !== -1 && str.value.match(val_date_time)) {
                        t = document.createTextNode('match');
                        spanElem.className = "pass";
                    } else {
                        t = document.createTextNode('no match');


                    }
                }
                else
                {
                    t = document.createTextNode('no value added');

                }
                str.parentNode.appendChild(spanElem);
                spanElem.appendChild(t);
            }
            validatethis();
        </script>
    </body>
</html>
Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...